jQuery each function

Link for all dot net and sql server video tutorial playlists<br /> <a href="https://www.youtube.com/user/kudvenkat/playlists?sort=dd&amp;view=1" title="https://www.youtube.com/user/kudvenkat/playlists?sort=dd&amp;view=1" target='_blank'>https://www.youtube.com/user/kudvenkat/playlists?sort=dd&amp;view=1</a><br /> <br /> Link for slides, code samples and text version of the video<br /> <a href="http://csharp-video-tutorials.blogspot.com/2015/04/jquery-each-function.html" title="http://csharp-video-tutorials.blogspot.com/2015/04/jquery-each-function.html" target='_blank'>http://csharp-video-tutorials.blogspot.com/2015/04/jquery-each-function.html</a><br /> <br /> 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.<br /> <a href="https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1" title="https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1" target='_blank'>https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1</a><br /> <br /> In this video we will discuss <br /> 1. Use of jQuery each function<br /> 2. How to exit from each function in jQuery<br /> 3. Implicit iteration in jQuery<br /> 4. Performance considerations when using jquery each function<br /> <br /> jQuery each function is used to iterate over the items in a collection. For each item in the collection the anonymous function is called. The index of the element and the element itself are passed to the anonymous function. <br /> <br /> $(&#039;li&#039;).each(function (index, element) {<br /> alert(index + &#039; : &#039; + $(element).text());<br /> });<br /> <br /> To refer to the current element that we are iterating over you can also use this keyword.<br /> $(&#039;li&#039;).each(function (index) {<br /> alert(index + &#039; : &#039; + $(this).text());<br /> });<br /> <br /> If you don&#039;t care about the index and just need the text of the list item, then you can get rid of the index parameter<br /> $(&#039;li&#039;).each(function () {<br /> alert($(this).text());<br /> });<br /> <br /> Replace &amp;lt; with LESSTHAN symbol and &amp;gt; with GREATERTHAN symbol<br /> <br /> How to exit from each function in jQuery : To exit from each function, return false.<br /> &amp;lt;script type=&quot;text/javascript&quot;&amp;gt;<br /> $(document).ready(function () {<br /> $(&#039;li&#039;).each(function () {<br /> if ($(this).text() == &#039;UK&#039;) {<br /> return false;<br /> }<br /> alert($(this).text());<br /> });<br /> });<br /> &amp;lt;/script&amp;gt;<br /> <br /> Implicit iteration in jQuery : The $(&#039;li&#039;) selector returns all list item elements. Notice that we are calling the css() jquery function on the jquery collection itself. In this case, css() method implicitly iterates over each element in the entire collection. <br /> <br /> &amp;lt;html&amp;gt;<br /> &amp;lt;head&amp;gt;<br /> &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;<br /> &amp;lt;script src=&quot;jquery-1.11.2.js&quot;&amp;gt;&amp;lt;/script&amp;gt;<br /> &amp;lt;script type=&quot;text/javascript&quot;&amp;gt;<br /> $(document).ready(function () {<br /> $(&#039;li&#039;).css(&#039;color&#039;, &#039;red&#039;);<br /> });<br /> &amp;lt;/script&amp;gt;<br /> &amp;lt;/head&amp;gt;<br /> &amp;lt;body style=&quot;font-family:Arial&quot;&amp;gt;<br /> &amp;lt;ul&amp;gt;<br /> &amp;lt;li&amp;gt;US&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;India&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;UK&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;Canada&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;Australia&amp;lt;/li&amp;gt;<br /> &amp;lt;/ul&amp;gt;<br /> &amp;lt;/body&amp;gt;<br /> &amp;lt;/html&amp;gt;<br /> <br /> There is no need to explicitly iterate over each element in the collection. Most jQuery methods implicitly iterate over the entire collection.<br /> &amp;lt;script type=&quot;text/javascript&quot;&amp;gt;<br /> $(document).ready(function () {<br /> $(&#039;li&#039;).each(function () {<br /> $(this).css(&#039;color&#039;, &#039;red&#039;);<br /> });<br /> });<br /> &amp;lt;/script&amp;gt;<br /> <br /> Performance considerations when using jquery each function<br /> &amp;lt;html&amp;gt;<br /> &amp;lt;head&amp;gt;<br /> &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;<br /> &amp;lt;script src=&quot;jquery-1.11.2.js&quot;&amp;gt;&amp;lt;/script&amp;gt;<br /> &amp;lt;script type=&quot;text/javascript&quot;&amp;gt;<br /> $(document).ready(function () {<br /> $(&#039;li&#039;).each(function () {<br /> $(&#039;#divResult&#039;).html($(&#039;#divResult&#039;).html() + &#039;&amp;lt;br/&amp;gt;&#039; + $(this).text())<br /> });<br /> });<br /> &amp;lt;/script&amp;gt;<br /> &amp;lt;/head&amp;gt;<br /> &amp;lt;body style=&quot;font-family:Arial&quot;&amp;gt;<br /> &amp;lt;ul&amp;gt;<br /> &amp;lt;li&amp;gt;US&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;India&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;UK&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;Canada&amp;lt;/li&amp;gt;<br /> &amp;lt;li&amp;gt;Australia&amp;lt;/li&amp;gt;<br /> &amp;lt;/ul&amp;gt;<br /> &amp;lt;div id=&quot;divResult&quot;&amp;gt;&amp;lt;/div&amp;gt;<br /> &amp;lt;/body&amp;gt;<br /> &amp;lt;/html&amp;gt;<br /> <br /> From a performance standpoint, there are 2 problems with the above code.<br /> 1. jQuery needs to search the DOM for div element with id = divResult, for each list item in the collection. Since there are 5 list items, jquery searches the DOM 5 times for the same div element with id = divResult. The performance of the above code can be improved by caching the divResult element.<br /> <br /> 2. The DOM element (div element with id = divResult) is being updated on each iteration. Again this is bad for performance. To improve the performance build a string variable with the required data on each iteration of the loop. Once the loop has completed, update the DOM element with the value that is present in the string varibale. This will ensure that the DOM element is updated only once and this is much better for performance.<br /> <br /> The following is much better from a performance standpoint.<br /> &amp;lt;script type=&quot;text/javascript&quot;&amp;gt;<br /> $(document).ready(function () {<br /> var result = &#039;&#039;;<br /> $(&#039;li&#039;).each(function () {<br /> result += &#039;&amp;lt;br/&amp;gt;&#039; + $(this).text();<br /> });<br /> $(&#039;#divResult&#039;).html(result);<br /> });<br /> &amp;lt;/script&amp;gt;<i class="fa fa-language transViewIcon clickable" title="Translation"></i>

jQuery each function
Video date 2015/04/09 05:04
Play musics without ads!
jQuery each function
Once shared, this message disappears.
https://i.ytimg.com/vi/2G4nooOPj1U/mqdefault.jpg
https://www.youtube.com/embed/2G4nooOPj1U
jQuery each function
808
15:37jQuery each function
jQuery each function
00:00
Loading...
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/jquery-each-function.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 this video we will discuss
1. Use of jQuery each function
2. How to exit from each function in jQuery
3. Implicit iteration in jQuery
4. Performance considerations when using jquery each function

jQuery each function is used to iterate over the items in a collection. For each item in the collection the anonymous function is called. The index of the element and the element itself are passed to the anonymous function.

$('li').each(function (index, element) {
alert(index + ' : ' + $(element).text());
});

To refer to the current element that we are iterating over you can also use this keyword.
$('li').each(function (index) {
alert(index + ' : ' + $(this).text());
});

If you don't care about the index and just need the text of the list item, then you can get rid of the index parameter
$('li').each(function () {
alert($(this).text());
});

Replace &lt; with LESSTHAN symbol and &gt; with GREATERTHAN symbol

How to exit from each function in jQuery : To exit from each function, return false.
&lt;script type="text/javascript"&gt;
$(document).ready(function () {
$('li').each(function () {
if ($(this).text() == 'UK') {
return false;
}
alert($(this).text());
});
});
&lt;/script&gt;

Implicit iteration in jQuery : The $('li') selector returns all list item elements. Notice that we are calling the css() jquery function on the jquery collection itself. In this case, css() method implicitly iterates over each element in the entire collection.

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script src="jquery-1.11.2.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$(document).ready(function () {
$('li').css('color', 'red');
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body style="font-family:Arial"&gt;
&lt;ul&gt;
&lt;li&gt;US&lt;/li&gt;
&lt;li&gt;India&lt;/li&gt;
&lt;li&gt;UK&lt;/li&gt;
&lt;li&gt;Canada&lt;/li&gt;
&lt;li&gt;Australia&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;

There is no need to explicitly iterate over each element in the collection. Most jQuery methods implicitly iterate over the entire collection.
&lt;script type="text/javascript"&gt;
$(document).ready(function () {
$('li').each(function () {
$(this).css('color', 'red');
});
});
&lt;/script&gt;

Performance considerations when using jquery each function
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script src="jquery-1.11.2.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$(document).ready(function () {
$('li').each(function () {
$('#divResult').html($('#divResult').html() + '&lt;br/&gt;' + $(this).text())
});
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body style="font-family:Arial"&gt;
&lt;ul&gt;
&lt;li&gt;US&lt;/li&gt;
&lt;li&gt;India&lt;/li&gt;
&lt;li&gt;UK&lt;/li&gt;
&lt;li&gt;Canada&lt;/li&gt;
&lt;li&gt;Australia&lt;/li&gt;
&lt;/ul&gt;
&lt;div id="divResult"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

From a performance standpoint, there are 2 problems with the above code.
1. jQuery needs to search the DOM for div element with id = divResult, for each list item in the collection. Since there are 5 list items, jquery searches the DOM 5 times for the same div element with id = divResult. The performance of the above code can be improved by caching the divResult element.

2. The DOM element (div element with id = divResult) is being updated on each iteration. Again this is bad for performance. To improve the performance build a string variable with the required data on each iteration of the loop. Once the loop has completed, update the DOM element with the value that is present in the string varibale. This will ensure that the DOM element is updated only once and this is much better for performance.

The following is much better from a performance standpoint.
&lt;script type="text/javascript"&gt;
$(document).ready(function () {
var result = '';
$('li').each(function () {
result += '&lt;br/&gt;' + $(this).text();
});
$('#divResult').html(result);
});
&lt;/script&gt;
View comments
This playlist has no title.
jQuery each function
Share with your friends!
Press emoticons to leave feelings.
#Like
#Like
0
#Funny
#Funny
0
#Sad
#Sad
0
#Angry
#Angry
0
#Cool
#Cool
0
#Amazing
#Amazing
0
#Scary
#Scary
0
#Want more
#Want more
0
114343 https://www.youtube.com/watch?v=2G4nooOPj1U jQuery each function 3
Mark LIKE on the tags!
420442 jquery each function example
420443 jquery each function index
420444 jquery each function break
420445 jquery each function exit
420446 jquery each function parameters
420447 jquery each function performance
420448 jquery each function pass parameter
420449 jquery implicit iteration
420450 jquery each index
346999 j
1
522949 jquery each index element
522950 jquery each example
522951 jquery each break out
522952 jquery tutorial
522953 jquery tutorial for beginners
522954 divResult
Vlogger
Vlogger
8K+
7K+
Subscribe Popular Videos! :)
 
Share page of @Vlogger
UnMark |Edit |Search
Mark |Dislike |Search
Mark |Del |Search
Open
Report
Full screen
Timer
Translation