Monday, June 14, 2010

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. 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.