1.11M Members

how to get the value of jquery generated radio button

 
0
 

i am trying to get the value of radio button which is generated by jquery. I think there is some problem in call events. Here is my code.

HTML
<div id="divOption1"></div>

Jquery radio button generate
document.getElementById('divOption1').innerHTML = '<input id="option1" type="radio" value="1"/> '+ questions[currentQuestion]['option1'];

here i call jquery function to get radio button value

$("#option1").click(function (){
        alert($(this).val());
    })

help me to figure out where is the problem.

 
0
 

The click event is bound upon DOM ready state. If you change the DOM thereafter by adding new elements, these will not have the click event bound to them.

You can either, rebind the click event (a little pointless), or you could use the on function.

$(document)
    .on({click: function(event) {
        console.log($(this).val());
        alert($(this).val());
    }}, '#option1')
 
0
 

write your codein document.ready as:

document.getElementById('divOption1').innerHTML = '<input id="option1" type="radio" value="1"/> ';
$("#option1").click(function (){
        alert($(this).val());
    })
});

it works!!

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: