|
|
Modulus % of a division We can get modulus of a any two numbers in JavaScript by using % . This gives us the reminder after a division. For example let us find out modulus of 23 and 5. Here is the sample.
var x=123%5;
document.write(x);
Output is 3
Similarly the following code will give output of 0
var x=12%2;
document.write(x);
Using this we can develop one application where for even numbers of record counter we will make the background color of the record a different color. This way we will get alternate colors for the records to display. This is part of a script to show the application of modulus and not a fully working script.
var color="#f1f1f1";
for(i=0;i<myObject.data.length;i++)
{
if((i%2)==0){color="#ffffff";}
else{color="#f1f1f1";}
str = str + "<tr bgcolor="+color+"><td>" + myObject.data[i].gal_id +
" </td><td><a href=../g_view_gal_th.php?gal_id="+myObject.data[i].gal_id+
" target=new>" + myObject.data[i].gal_name + "</a></td><td>" +
myObject.data[i].userid + " </td></tr>"
}
Found anything wrong or wants to improve the code by adding more features? Post your short comment here or use the Forum
| |
| | |
|
|
|
|
|