0

I have a sample work done in JSFiddle and I have updated the code by adding a radio button. If selected, I would like to add some text next to the selected radio button. When I clicked, it seems not even geting alert working. Could someone help on what I am doing wrong?

http://jsfiddle.net/XRUX8/4/

 $('input[name=primaryAgent]:radio').click(function(){
    alert("test alert");
    $("#note").text("Helllo next by Primary");
 });
PSL
  • 123,204
  • 21
  • 253
  • 243
user2088016
  • 99
  • 2
  • 2
  • 9
  • possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – PSL Oct 08 '13 at 22:05
  • Its a duplicate this is what you need http://jsfiddle.net/f27YD/ – PSL Oct 08 '13 at 22:05
  • 2
    i dont see no radio button ! – Hussein Nazzal Oct 08 '13 at 22:06
  • @HusseinNazzal you need to click on the button.. :) See the fiddle. It is classic question, binding event on dynamically created element. – PSL Oct 08 '13 at 22:07
  • Button named radio :-) – zod Oct 08 '13 at 22:07
  • aha .. sorry i saw the fiddle but couldn't locate the radio button code .. because of the too much line breaks . :/ – Hussein Nazzal Oct 08 '13 at 22:09
  • Problem fixed. Thank you all. I am new bee learing Jquery bootstratp CSS :). Thanks once again. – user2088016 Oct 08 '13 at 22:12
  • Small issue, why it is deleting remove button? Is theranyway I can keep the text and button aswell ? – user2088016 Oct 08 '13 at 22:16
  • Hello, If I add two names and when I click on either one of those radio buttons it is always updating the first
    tag. I know this is due to dynamically creating elements and both may have same DIV ID and then first one get written. How can we track which radio button was click and write next by it? http://jsfiddle.net/XRUX8/10/
    – user2088016 Oct 08 '13 at 22:36

1 Answers1

1

You need to use .on to bind events to dynamically generated elements. Your event listener for the radio button should look like this.

$(document).on('click', 'input[name=primaryAgent]:radio',function(){
    alert("test alert");
    $("#note").text("Hello next by Primary");

});
joe42
  • 657
  • 4
  • 12