javascript - styling using jquery based on condition -
i have following html code, have given background color red #list . if number of li more 5, want change background blue. please assist
<html> <head> <script src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ var len= $("#list li").size(); if(len>5){ // need code style #list here if number of li more 5, background blue } }); </script> </head> <body> <ul id="list" style="background:red; width:300px;height:300px;"> <li>image 1</li> <li>image 2</li> <li>image 3</li> <li>image 4</li> <li>image 5</li> <li>image 6</li> <li>image 7</li> <li>image 8</li> <li>image 9</li> <li>image 10</li> </ul> </body> </html>
you'll want use .length
instead of size()
.
var len= $("#list li").length; if(len>5){ $("#list").css("background-color","blue"); //use .css() change background color }
Comments
Post a Comment