Split function in jQuery

jQuery provides a method "split()", which splits the text. We can use any delimiter to split the text. Let me explain you with an example.

Earlier I had posted about jQuery solution to replace string, substring, trim string and about all string functions, And In this post, see jQuery/JavaScript split function in action with example.

First declare a label and assign some text to it. We will split the text using space as delimiter.

<asp:Label ID="lblMessage" runat="server"
Text="jQuery By Example Rocks!!" />

See below jQuery code, which makes use of split function and split the string with space.

<script type="text/javascript">
$(document).ready(function(){
   var element = $("#lblMessage").text().split(" "); 
   alert(element);
});
</script>

Thing to note here is the variable "element" declared in jQuery code becomes an array and you can easily iterate through it. If you say alert(element[0]) then it will alert jQuery.

See live Demo and Code

Read my other helpful posts:

Feel free to contact me for any help related to jQuery. I will gladly help you.