If we are using ASP.NET Radiobuttonlist in our page it becomes a little difficult to find the instance of it through Javascript because it is rendered in a different way in the browser ( the code is reproduced below )
ASP.net Code
—————–
Is rendered in browser as :
AAA |
BBB |
Blank
So to access the first instance of the Radiobutton you can search for the elementsByName
The following javascript describe how to select the last radio button from the list.
function SelectRadioButton() { var radiobutton = document.getElementsByName(''); var i = radiobutton.length; radiobutton[i - 1].checked = true; //Represent the last radio button. }
N.B. As different browser render in different way the Radiobuttonlist . I.E retrives 1 more element in the element array than other browsers so we have to acess the elements like the above way.
If your radiobutton list is present inside a content place holder same radiobutton list may be rendered as following
AAA |
BBB |
so in that case we have to replace the ‘_’ with ‘$’ before accesing the elements.
function SelectRadioButton() { var rblName = ('').replace(/_/g, '$'); var radiobutton = document.getElementsByName(rblName); var i = radiobutton.length; radiobutton[i - 1].checked = true; //Represent the last radio button. }
It is one way by which we can acess instance of each radio button from the radiobuttonlist froms Javascript.