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.

Recommended Answers

All 2 Replies

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')

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!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.