Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Difference between $ each and  each
Play lesson

jQuery tutorial for beginners - Difference between $ each and each

5.0 (2)
25 learners

What you'll learn

This course includes

  • 22.5 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

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 Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 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.

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere