1,105,363 Community Members

how to get the value of jquery generated radio button

Member Avatar
aneeqtariq_143
Newbie Poster
18 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
blocblue
Practically a Posting Shark
828 posts since Jan 2008
Reputation Points: 238 [?]
Q&As Helped to Solve: 162 [?]
Skill Endorsements: 16 [?]
Featured
 
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')
Member Avatar
ruchi18
Light Poster
46 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: