Skip navigation

Monthly Archives: June 2009


Consider the below code

//
aAge = Array(31,23,42,44,25);  //1
aLength = aAge.length;  //2
for(i=0;i<aLength;i++)   //3
  alert(aAge[i]);   //3
//

How to avoid line number 2?

//
aAge = Array(31,23,42,44,25);  //1
for(i=0;i<aAge.length;i++)   //2
  alert(aAge[i]);   //3
//

But to avoid length calculation each iteration

//
aAge = Array(31,23,42,44,25);  //1
for(i=0,aLength=aAge.length;i< aLength;i++)   //2
  alert(aAge[i]);   //3
//

A cool trick!! May be useful to you guys!!!


Add this into your head section

<script type="text/javascript" src="includes/jquery-1.3.2.min.js"></script>

Add this into your body section

<table width="500" border="1" cellspacing="0" cellpadding="4">
  <tr>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<p>
<input value="First Column" />
<input id="secondColumn" value="Second Column" />
<input value="Third Column" />
<input value="Reset" />
</p>
<script>
$(function(){
  // Change first column background color.
  $("#firstColumn").click(function(){
  $("table tr td:first-child").css("background-color","#ff0000");
});
// Change second column background color.
$("#secondColumn").click(function(){
  $("table tr td:nth-child(2)").css("background-color","#ff0000");
});
// Change third column background color.
$("#thirdColumn").click(function(){
  $("table tr td:last-child").css("background-color","#ff0000");
});
// reset background color.
$("#reset").click(function(){
  $("table tr td").css("background-color","transparent");
});
})
</script>

Create an object

var myObject = {};

Add members using dot notation

myObject.name = "vkanakaraj";

Add members using subscript notation

myObject["function"] = "shock";
myObject.if = "will work because this is js" ;

You can create members with reserved words!!!!

Follow

Get every new post delivered to your Inbox.

Join 150 other followers