JQuery add button inside -


i want have below: have text box(label survey qs), button(add choice), radiobox+ text buttons(add button) @ bottom. when click on add button, new div getting created (like survey2). when click on add choice(1st one), new radio+text box created.

however when click on add choice(second one), radio+text box not created. reason handlers not associated when create new objects. not able put live() method code so.

i novice. please help. in advance.

please find code:

<html> <head> <title>jquery add / remove textbox example</title>  <script type="text/javascript" src="jquery.js"></script>  <style type="text/css">     div{         padding:8px;     } </style>  </head>  <body>  <h1>jquery add / remove textbox example</h1>  <script type="text/javascript">  $(document).ready(function(){      var counter = 2;     var choicecounter=2;      $("#addbutton").click(function () {     //$("#addbutton").live("click",function () {     //alert("inside click function");     choicecounter=0;     if(counter>10){             alert("only 10 textboxes allow");             return false;     }         var newtextboxdiv = $(document.createelement('div'))          .attr("id", 'textboxdiv' + counter);      newtextboxdiv.after('<label>survey question '+ counter + ' : </label>' + '<input type="text" name="textbox' + counter +  '" id="textbox' + counter + '" value="" >');      newtextboxdiv.append('<label>survey question ' + counter + ' : </label>' + '<input type="text" name="textbox'  + counter + '" id="textbox' + counter + '" value="" /> <input type="button" value="add choice" id="addoption"> </br><input type="radio"> <input type="textbox" id="option1" ></br>');              $('#textboxesgroup').append(newtextboxdiv);         newtextboxdiv.appendto("#textboxesgroup");       counter++;      });        $("#addoption").click(function () {   // new add method     alert("inside option function");     if(choicecounter>10){             alert("only 10 textboxes allow");             return false;     }         var newtextboxdiv = $(document.createelement('div'))          .attr("id", 'textboxdiv' + choicecounter);      newtextboxdiv.after( '<br><input type="radio"> <input type="text" name="textbox' + choicecounter + '" id="option' + choicecounter + '" value="insert choice" >');       newtextboxdiv.append( '<br><input type="radio"> <input type="text" name="textbox' + choicecounter + '" id="option' + choicecounter + '" value="insert choice" />');                    $('#textboxesgroup').append(newtextboxdiv);        newtextboxdiv.appendto("#textboxesgroup");      choicecounter++;      });           $("#removebutton").click(function () {     if(counter==1){           alert("no more textbox remove");           return false;        }         counter--;          $("#textboxdiv" + counter).remove();       });       $("#getbuttonvalue").click(function () {      var msg = '';     for(i=1; i<counter; i++){       msg += "\n textbox #" + + " : " + $('#textbox' + i).val();     }           alert(msg);      });   }); </script> </head><body>  <div id='textboxesgroup'>     <div id="textboxdiv1">          <label>survey question 1: </label><input type='textbox' id='textbox1' >  <input type="button" value='add choice' id="addoption">         <br><input type="radio"> <input type='textbox' id='option1' ><br>         </div> </div> <input type='button' value='add button' id='addbutton'> <input type='button' value='remove button' id='removebutton'> <input type='button' value='get textbox value' id='getbuttonvalue'>  </body> </html> 

try delegate event:

$("#textboxesgroup").on("click", "#addoption", function () {    // code }): 

or can try

$("#textboxesgroup").delegate("#addoption", "click", function () {     // code }): 

a little note

.on() ordinary binding used like

$(target).on(eventname, handlerfunction) 

but delegate event (aka live) use like

$(container).on(eventname, target, handlerfunction) 

here container point holder of target belong dom @ page load. container , target valid jquery selector.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -