This is in ASP.NET with C#. I am trying to programmatically call a Javascript function from a radiobuttonlist click event. I have created the radiobuttonlist and added a couple of list items inside. I have looked around a lot but have been unable to figure this out.
C# code:
RadioButtonList _rbList = new RadioButtonList();
_rbList.ID = r[0].ToString()+"Answer";
_rbList.RepeatDirection = RepeatDirection.Horizontal;
_rbList.Attributes.Add("onchange", "return answered();");
Javascript inside my .aspx:
<script type ="text/javascript">
function answered() {
alert("I am an alert box!");
}
</script>
You adding the onchange on the RadioButtonList…
Add it to the RadioButton inside of RadioButtonList and it will work, which in lower level speaking, adding attribute to the ListItem from Items from RadioButtonList, just one problem…
adding
"onchange"attribute will be render a<span>containing the<input radio, pretty gross I know, try to addonclickinstead, so the render can attach the attribute to the<input radio,and do your magic on the javascript click, something like that: