Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attaching JavaScript to radio buttons 1

Status
Not open for further replies.

mais123

Programmer
Dec 26, 2007
36
US
Hi, I have radio button group with 2 buttons, Yes and No. Few things:
1) How do I attach JS event to fire when user clicks Yes
2) In JS code, how can I check the value of the radio button group, basically if Yes or No is selected?
I am using ASP.NET radio button group, but here is the HTML generated

Code:
<table id="rdReseller"  border="0">
	<tr>
		<td><input id="rdReseller_0" type="radio" name="rdReseller" value="0" /><label for="rdReseller_0">Yes</label></td><td><input id="rdReseller_1" type="radio" name="rdReseller" value="1" checked="checked" /><label for="rdReseller_1">No</label></td>
	</tr>
</table>

Thanks!!!
 
It can get done in more than one way. This is one amongst the others. (I use window.onload to add it, it is for illustration only.)
[tt]
<script language="javascript">
function doit() {
var oelem;
oelem=document.getElementById("rdReseller_0");
if (document.attachEvent) {
oelem.attachEvent("onclick",handler);
} else if (document.addEventListener) {
oelem.addEventListener("click",handler,false);
}
//a bit clusmy to repeat script block, just for illustration
oelem=document.getElementById("rdReseller_1");
if (document.attachEvent) {
oelem.attachEvent("onclick",handler);
} else if (document.addEventListener) {
oelem.addEventListener("click",handler,false);
}
}
function handler(evt) {
if (window.event) {
alert(window.event.srcElement.id);
} else {
alert(evt.target.id);
}
}
window.onload=doit; //illustration only
</script>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top