javascript - JS - jQuery dynamically add and remove fields -


i trying make dynamically duplicated field jquery :

the html (php) code :

<div  id="widget_dup">     <p>      <textarea class="code" cols="50" rows="5" id="o99_brsa_settings[brsa_dash_wdgt_content]"  name="o99_brsa_settings[brsa_dash_wdgt_content]"  value="<?php //echo $o99_brsa_options['brsa_dash_wdgt_content']; ?>"/><?php                          echo $o99_brsa_options['brsa_dash_wdgt_content'];                          ?></textarea>                         <label class="description" for="o99_brsa_settings[brsa_dash_wdgt_content]">                         </br><?php _e('content 1st widget', 'o99-brsa-domain'); ?>                         </label>      </p> </div> <div id="addscnt">add</div><div id="remscnt">remove</div> 

the js assembled snippets found :

<script type="text/javascript">                  //http://jsfiddle.net/obmero99/zd9ky/                      // <![cdata[                      jquery(function() {                             var scntdiv = jquery('#widget_dup');                             var prevdiv = scntdiv.html();                              var = jquery('#widget_dup p').size() + 1;                              jquery('#addscnt').live('click', function() {                                      jquery(prevdiv).appendto(scntdiv);                                      i++;                                     //alert (prevdiv);                                     return false;                             });                              jquery('#remscnt').live('click', function() {                                      if( > 2 ) {                                             jquery(this).parents('p').remove();                                             i--;                                     }                                     return false;                             });                     });                      // ]]>                 </script> 

the problem code duplicate fields ok, without changing id , names , other attributes ( id, name ,for etc.. )

i have tried : jquery(this).attr('id')+i; , jquery(this).attr('name')+i;, not work intend. infact, not work @ all. :-)

how can modify attr() of field while theory not exists yet ?

as per comments: fiddle here: http://jsfiddle.net/obmerk99/uzuwa/

the correct use of setting attribute is

$(this).attr('id', i); 

you should know, better use clone(), ian suggested!

demo .clone

http://jsbin.com/oqalig/1/edit


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -