Feb 08

New jdk 1.5 ‘for’ loop ( Enhanced for loop-’for each’)

int a[] = {1,2,3,4,5};

for(int i =0;i<a.length;i++)


System.out.println(“Value: “+a[i]);


This is so 1.4!!!

Here’s how you do it with the new and enhanced for loop :

for(int b: a)


System.out.println(“Value :”+b);


To make it easy for you to understand how to interpret the above condition, read it as follows:

” for each int b in a…..iterate”

Basically what you have to do is – To the left of the colon in the for loop declare the datatype that is present inside the array and to the right of the colon write the name of the array.

The above ‘for’ loop will iterate as many times as there are elements in the array ‘a’. At every iteration int b will have the value of the next element.

In simple terms its like this:

for(int b: a) will internally expand like this : for(int i=0;i<a.length;i++){int b=a[i];}

You can use this for loop for iterating over type collections too.

Example :

List<MyObject> al = new ArrayList();

//Create an Object of some class you want to put into the arraylist.

MyObject myObject = new MyObject();

MyObject myObject1 = new MyObject();



for(MyObject o : al)


System.out.println(“Object hashcode is “+o.hashCode());


Feb 08

Disable(readOnly) and Enable textbox using JavaScript

The following is a short example of a code that will enabledisable a textbox at the click of a button :



function enableDisable()


//This will get the HTML content of the current page

var form = document.testEnableDisable;

//The if condition checks for the value of the button.


//If button value is ‘Disable’ then the textbox is made readOnly

form.textBox1.readOnly = true;

//The button value is changed to ‘Enable’





form.textBox1.readOnly = false;






<form name= “testEnableDisable”>

<input type = “text” id = “textBox1″ value=”Enabled”>

<input type=”button” id=”enableDisableButton” onClick=”enableDisable()” value=”Disable”>




Basically, its the readOnly value of the text box that needs to be changed using JavaScript.

Note: JavaScript is case sensitive and hence ‘readonly’ will not give you the right result…‘O’ should be in capital‘readOnly’ when you use it in JavaScript.

Click here to view the example

Feb 08

Allow Users to enter data into Message Box(Javascript Prompt)

Javascript has a function named ‘prompt’ which displays a message box along with a text box in it which allows users to input data.

Here’s how you achieve it :

function testPrompt()


var enteredData = prompt(“Type something in the text box below”,”");

alert(“You entered “+enteredData);


prompt() takes two parameters. The first parameter is used to display the message above the text box whereas the second parameter fills up the textbox with the values you specify(leave it blank if you want the text box to be empty)

To get a quick glimpse of the promt box copy paste the code below into your browser address bar and press enter :

Note: you’ll have to manually replace the double quotes(with double quotes again) after you copy paste the code due to the formatting in this page

javascript:var s = prompt(“Type something in the text box below”,”");alert(s);