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.

3
Contributors
2
Replies
20
Views
4 Years
Discussion Span
Last Post by ruchi18
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!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.