We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,672 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to get the value of jquery generated radio button

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
21 Hours
Discussion Span
2 Months Ago
Last Updated
19
Views
aneeqtariq_143
Newbie Poster
17 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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')
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

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

ruchi18
Light Poster
46 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0584 seconds using 2.67MB