PDA

View Full Version : how to access checkbox text through javascript



sugeet_s
Jun 8th, 2006, 08:43 AM
hi friends,
I have following code generated through asp.net using javascript

<input id="chkInterest1" type="checkbox" name="chkInterest1" /><label for="chkInterest1">Interest1</label>

now i want to access the text Interest1 so how can i access it using javascript.


thanks in advance

regards
sugeet

Kravvitz
Jun 8th, 2006, 10:25 AM
Try this. The second line is there because of the differences in the way different browsers handle white space characters in the document.

var lbl = document.getElementById('chkInterest1').nextSibling;
while(lbl.nodeName.toLowerCase()!='label') lbl=lbl.nextSibling;

sugeet_s
Jun 8th, 2006, 11:31 AM
hi kravvitz,

You gave me the code to access the variable but not its value. how to access its value that is Interest1 of the label.

I converted the nodename to string, it gave me LABEL but not the value inside the label.


thanks for ur reply

regards

sugeet

sugeet_s
Jun 8th, 2006, 11:33 AM
hi karvvitz,


I got the value by using lbl.innerHTML.

thanks buddy for your reply, keep in touch



regards
sugeet

Kravvitz
Jun 9th, 2006, 05:51 AM
You're welcome :)

To avoid using the non-standard, though widely supported innerHTML, you could use the following instead.

lbl.firstChild.nodeValue
OR

lbl.firstChild.data

sugeet_s
Jun 9th, 2006, 06:08 AM
thanks once again,


i have one more problem. Here is a code generated by asp.net for checkbox list.

<table id="chkListInterest" border="0" style="z-index: 100; left: 424px;
position: absolute; top: 40px">
<tr>
<td><input id="chkListInterest_0" type="checkbox" name="chkListInterest$0" onclick="javascript:validateCheckbox();" /><label for="chkListInterest_0">Interest 1(ar)</label></td>
</tr><tr>
<td><input id="chkListInterest_1" type="checkbox" name="chkListInterest$1" onclick="javascript:validateCheckbox();" /><label for="chkListInterest_1">Interest 2(ar)</label></td>
</tr><tr>
<td><input id="chkListInterest_2" type="checkbox" name="chkListInterest$2" onclick="javascript:validateCheckbox();" /><label for="chkListInterest_2">Interest 3(ar)</label></td>
</tr><tr>
<td><input id="chkListInterest_3" type="checkbox" name="chkListInterest$3" onclick="javascript:validateCheckbox();" /><label for="chkListInterest_3">Interest 4(ar)</label></td>
</tr><tr>
<td><input id="chkListInterest_4" type="checkbox" name="chkListInterest$4" onclick="javascript:validateCheckbox();" /><label for="chkListInterest_4">Interest 5(ar)</label></td>
</tr>
</table>


Now i want that the user should select only 4 checkboxes and not more than that. I had wrtten following javascript function but it didn't work. do u have any suggestions.



function validateCheckbox()
{
alert(indexForCheckbox);

var chkListInterest = document.getElementsById('chkListInterest');
// chkListInterest.innerHTML;
alert(chkListInterest);
var chkListInterestTr = chkListInterest.getElementsByTagName('tr');
alert(chkListInterestTr);
var chkListInterestInput = chkListInterestTr.getElementsByName('chkListInterest$0');
alert(chkListInterestInput.nodeName.toString());



while(indexForCheckbox <5)
{

while(chkListInterest.nodeName.toString()!='input')
{
chkListInterest=chkListInterest.nextSibling;
alert(chkListInterest);
}

if(chkListInterest.checked = checked)
{
alert(indexForCheckbox);
indexForCheckbox++;
}
}

if(indexForCheckbox >= 3 )
{
alert(Preference_Err1);
return false;
}

}


thanks for ur reply


regards

sugeet

Kravvitz
Jun 9th, 2006, 06:56 AM
1) Where does indexForCheckbox come from?

2) Why are you using a <table>? That looks like it should be a list (<ul>).

3) Change onclick="javascript: to just onclick="

4) Why are you using nodeName.toString() instead of just nodeName?