Saturday, June 14, 2008

Generate HTML dynamically in javascript

Place a div tag in then body of the html where you the dynamic html to be displayed


Suppose you want a textbox control to be added each time in an on click event of a hyper link,


Onclick javascript function:

You can use this logic according to your requirements.

Friday, June 13, 2008

To check and uncheck a radio button

Code to check and uncheck a radio button,

Logic 1:
Add below code to the control
onclick='this.chk = this.chk ? this.checked = !this.chk : this.checked'
OR

Explanation:

A property by name "chk" is created to "this" object and is assigned the value of standard "checked". It stores the current state of the radio button. Based on this we check and uncheck the radio button. (You can use any name in place of "chk")

Logic 2:


Explanation:
Declare a global variable and set it to null initially. Call this function in the radio button control onclick event and pass "this" as parameter. When radio button is checked for first time, the if condition is true and hence it is checked. The global variable's value is now changed. Now when you check the radio button, if condition is false and hence else part is executed where radio button checked property is set to false. Here, global variable's value is again changed. This way, the radio button gets checked and unchecked alternatively.