HomeОбразованиеRelated VideosMore From: kudvenkat

Difference between $ each and each

176 ratings | 47588 views
Link for all dot net and sql server video tutorial playlists https://www.youtube.com/user/kudvenkat/playlists?sort=dd&view=1 Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2015/04/difference-between-each-and-each.html In jQuery there are 2 versions of each() method 1. .each() 2. $.each() or jQuery.each() Please note : $ is shortcut for jQuery. What is the difference .each is used to iterate over the items in a jQuery collection Example : In this example, the jQuery selector $('li') returns a jQuery object collection. So to loop thru the objects in the jQuery collection we are using .each() method. Notice this method is called on the jQuery object collection, that the selector returns, so the .each() method knows the items it has to iterate over. The callback method has 2 parameters 1) index - The index of the element 2) element - The DOM element that we are currently iterating over Since the element is a raw DOM object, to use use jQuery methods you have to wrap it using the jQuery wrapper $ as shown below. $(element) Instead of using element parameter, you can also use this keyword, which also refers to the raw DOM element that we are currently iterating over. To use jQuery methods you have to wrap it using the jQuery wrapper $ as shown below. $(this) Replace < with LESSTHAN symbol and > with GREATERTHAN symbol. <html> <head> <title></title> <script src="jquery-1.11.2.js"></script> <script type="text/javascript"> $(document).ready(function () { var result = ''; $('li').each(function (index, element) { result += 'Index = ' + index + ', Value = ' + $(element).text() + '<br/>'; //or //result += 'Index = ' + index + ', Value = ' + $(this).text() + '<br/>'; }); $('#resultDiv').html(result); }); </script> </head> <body style="font-family:Arial"> <ul> <li>US</li> <li>India</li> <li>UK</li> <li>Canada</li> <li>Australia</li> </ul> <div id="resultDiv"></div> </body> </html> $.each() or jQuery.each() is used for iterating over javascript objects and arrays. Example : In the example below intArray is a JavaScript array. So to loop thru the elements in the array we are using $.each() function. Notice this function has 2 parameters 1) The JavaScript object or array that we want to iterate over 2) The callback function that will execute on each iteration If you want to use .each() instead of $.each() function on the intArray object, wrap it using the jQuery wrapper $ as shown below. $(intArray).each(function (index, element) { result += 'Index = ' + index + ', Value = ' + element + '<br />'; }) <html> <head> <title></title> <script src="jquery-1.11.2.js"></script> <script type="text/javascript"> $(document).ready(function () { var intArray = [100, 200, 300, 400, 500]; var result = ''; $.each(intArray, function (index, element) { result += 'Index = ' + index + ', Value = ' + element + '<br/>'; }); $('#resultDiv').html(result); }); </script> </head> <body style="font-family:Arial"> <div id="resultDiv"></div> </body> </html> Using $.each() to iterate over a JavaScript object : In this example, jsObject is a JavaScript object. In this example, $.each() is used to iterate over the JavaScript object properties. <html> <head> <title></title> <script src="jquery-1.11.2.js"></script> <script type="text/javascript"> $(document).ready(function () { var jsObject = { 'USA': 'Washington D.C.', 'India': 'New Delhi', 'UK': 'London', 'Australia': 'Canberra' }; var result = ''; $.each(jsObject, function (key, value) { result += key + ' - ' + value + '<br/>'; }); $('#resultDiv').html(result); }); </script> </head> <body style="font-family:Arial"> <div id="resultDiv"></div> </body> </html> In summary, .each() method is used to iterate over the items in a jQuery collection where as $.each() method is used to iterate over javascript objects or arrays.
Html code for embedding videos on your blog
Text Comments (11)
vatsal joshi (1 year ago)
its taking me a lot of time to watch and understand all these tutorials to cover full course so i am thinking how much time and efforts you put in to give us such a quality content respect ur efforts sir .
Shivkumar Gupta (1 year ago)
Thank You
Shivkumar Gupta (1 year ago)
great explanation with e.g.
Vimal Reddy (3 years ago)
is there online tutorials
Dushyant Sharma (2 years ago)
you deserve a medal.
Dmytro P (3 years ago)
Very cool!
ACLAproductions (3 years ago)
Very useful, thank you!
ArianitOn- (3 years ago)
the javascript object in the last example looks like json!!
Manoj Singh (2 years ago)
@Arianit - JSON is nothing but a format use to represent data in. As the name JSON - JavaScript Object Notation suggests, this format is basically a javascript object (javascript objects===JSON). And that is why that javascript object looks like JSON. Hope this helps.
developernader (3 years ago)
Thank you sir
Артем А (4 years ago)

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.