admin管理员组文章数量:1332389
i am trying to alert some text when a dynamically generated radio button is checked .. here is the link from fiddle .. /
function createRadioButtons(n)
{
$("#radioContainer").empty();
for(var i=0;i<n;i++)
{
radioButtons = "<p><input type='radio' class='"+n+"' name='"+n+"'>"+(i+1)+"</p>";
$("#radioContainer").append(radioButtons);
}
}
$("#dropDown").on("change",function()
{
createRadioButtons(parseInt($(this).val()));
});
$("#radioContainer input").on("change",function()
{
alert("checked");
});
when i click on radio button i am not getting alert .. can any one of you please help me in taking a look ?
Thanks in advance, Ashwin
i am trying to alert some text when a dynamically generated radio button is checked .. here is the link from fiddle .. http://jsfiddle/z7cu3q0y/
function createRadioButtons(n)
{
$("#radioContainer").empty();
for(var i=0;i<n;i++)
{
radioButtons = "<p><input type='radio' class='"+n+"' name='"+n+"'>"+(i+1)+"</p>";
$("#radioContainer").append(radioButtons);
}
}
$("#dropDown").on("change",function()
{
createRadioButtons(parseInt($(this).val()));
});
$("#radioContainer input").on("change",function()
{
alert("checked");
});
when i click on radio button i am not getting alert .. can any one of you please help me in taking a look ?
Thanks in advance, Ashwin
Share Improve this question asked Sep 11, 2014 at 6:37 Ashwin B ChandrappaAshwin B Chandrappa 111 silver badge2 bronze badges 1-
Plenty of answers below, so I'll just explain things. The problem you're having es from the fact that at the time you bind the event listener
$('#radioContainer input').on('change', ...)
, the radio button doesn't exist yet, so jQuery doesn't find it. The reason why the delegation answers below work is that you're binding the event listener to#radioContainer
, which does exist. Then when the click happens, you check to see if the clicked element was a radio button, which by this time does exist. – flowstoneknight Commented Sep 11, 2014 at 6:47
4 Answers
Reset to default 8Your code $("#radioContainer input").on("change",function(){})
will directly attach the event handler to the matching elements that are currently present in DOM
.
To work with dynamically generated elements added in the future, You need to delegate your event handler to a mon parent element:
$("#radioContainer").on("change","input",function(){
alert("checked");
});
The above will attach the handler to #radioContainer
, Whenever the corresponding event (change
in this case)is triggered (Usually propagated from the children), it checks whether the event target matches the specified selector (input
in this case) and invokes the handler accordingly.
Updated Fiddle
You need to use .on()
for dynamically generated elements like below. Here .on()
will bind change event to all radio buttons which are inside radioContainer
$("#radioContainer").on("change","input[type=radio]",function()
{
alert("checked");
});
Demo
Try using the below code
$(document).on("change","#radioContainer input",function(){
alert("checked");
});
Updated fiddle
You need to put the input defined as click area and radiocontainer as working area.
DEMO: http://jsfiddle/don/z7cu3q0y/3/
Just remove input before .on
and put inside of the function.
jQuery:
function createRadioButtons(n)
{
$("#radioContainer").empty();
for(var i=0;i<n;i++)
{
radioButtons = "<p><input type='radio' class='"+n+"' name='"+n+"'>"+(i+1)+"</p>";
$("#radioContainer").append(radioButtons);
}
}
$("#dropDown").on("change",function()
{
createRadioButtons(parseInt($(this).val()));
});
$("#radioContainer").on("change", 'input', function()
{
alert("checked");
});
本文标签: javascriptClick on dynamically generated radio buttonStack Overflow
版权声明:本文标题:javascript - Click on dynamically generated radio button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289405a2447519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论