PDA

View Full Version : chekbox text using java script



dpkbahuguna
Apr 14th, 2008, 04:56 PM
hello firnds..I am new in web development..pls tell me how to set the chkbox's text using java script

is there something so?

var objchk1 = document.getElementById("chk1");
objchk1.text = 'text';

thanks!!

glenngv
Apr 14th, 2008, 05:53 PM
Use <label> tag for the checkbox text.

<input type="checkbox" name="chk" id="mycheck" value="1" /><label id="lblChk" for="mycheck"></label>
Do this to set the label programmatically.

var objchkLbl1 = document.getElementById("lblChk");
objchkLbl1.innerHTML = 'whatever';

The good thing about using label tag for checkbox text is when you associate it with a checkbox using the for attribute, when you click the label, it is like clicking the checkbox.

Arbitrator
Apr 14th, 2008, 06:07 PM
objchkLbl1.innerHTML = 'whatever';An W3C DOM‐based alternative to glenngv’s second line of code would be objchkLbl1.appendChild(document.createTextNode("label"));.

dpkbahuguna
Apr 17th, 2008, 08:58 PM
Use <label> tag for the checkbox text.

<input type="checkbox" name="chk" id="mycheck" value="1" /><label id="lblChk" for="mycheck"></label>
Do this to set the label programmatically.

var objchkLbl1 = document.getElementById("lblChk");
objchkLbl1.innerHTML = 'whatever';

The good thing about using label tag for checkbox text is when you associate it with a checkbox using the for attribute, when you click the label, it is like clicking the checkbox.
thnaks friend....:) it solved my problem.