Yesterday I came around the situation to find the biggest li from the list of unordered list (UL). I have done that. And added a class which is the bigger in the list. Same can be apply to the ordered list (OL) too. I think it will be helpful for the others too. It is using jQuery as javascript framework.
If you have any questions or alternate solutions please let me know.
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Find the biggest from the unordered list or ordered list</title>
<script type='text/javascript' src='jquery-min.js'></script>
<script type="text/javascript">
$(document).ready(function() {
len=$("#list li").length;
bigger=0;
for(i=0;i<len;i++)
{
val1 = $("#list li").eq(i).text().length;
if(val1 > bigger)
{
bigger = val1;
pos = i;
}
else
{
bigger = bigger;
}
}
alert("postion "+ (pos + 1) + " is the biggest with " +bigger+ " characters");
$("#list li").eq(pos).addClass('bigger');
});
</script>
</head>
<body>
<ul id="list">
<li>123</li>
<li>1234</li>
<li>123456789</li>
<li>12345</li>
</ul>
</body>
</html> |
Loading ...
[...] Find the biggest from the unordered list or ordered list [...]
Alternate method would be
$(document).ready(function() { var biggest = 0; var bigIndex = 0; $("#list li").each(function(i,el){ var val = parseInt($(el).text()); if(val > biggest) { biggest = val; bigIndex = i } }); alert("postion "+ (bigIndex + 1) + " is the biggest"); $("#list li").eq(bigIndex).addClass('bigger'); });