javascript - Radio button onChange function -


i have small problem. wrong function? have no idea. need pop-up alert window when radio button selected. every reply.

html

<input type="radio" name="radiobutton" class="choice" value="1"> <input type="radio" name="radiobutton" class="choice" value="2"> <input type="radio" name="radiobutton" class="choice" value="3"> 

javascript

var formfields = {     init: function()     {          var radio = document.getelementsbyname("radiobutton");           radio.onchange = formfields.showalert;       },     showalert: function()     {                        alert("bye!");     }, }; 

because getelementsbyname returns nodelist you'll need apply event callback each input:

var radios = document.getelementsbyname("radiobutton"); for(var = 0;i < radios.length;i++){     radios[i].onchange = formfields.showalert; } 

demo: http://jsfiddle.net/louisbros/h7tdb/


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -