4

I am trying to make a dynamically duplicated field with 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 for 1st widget', 'o99-brsa-domain'); ?>
                        </label>

    </p>
</div>
<div id="addScnt">add</div><div id="remScnt">remove</div>

the JS was assembled from some snippets I found :

<script type="text/javascript">

                //http://jsfiddle.net/obmero99/ZD9Ky/

                    // <![CDATA[

                    jQuery(function() {
                            var scntDiv = jQuery('#widget_dup');
                            var prevDiv = scntDiv.html();

                            var i = 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( i > 2 ) {
                                            jQuery(this).parents('p').remove();
                                            i--;
                                    }
                                    return false;
                            });
                    });

                    // ]]>
                </script>

The problem is that this code will duplicate the fields ok, but without changing the ID , names and other attributes ( ID, NAME ,FOR etc.. )

I have tried to do : jQuery(this).attr('id')+i; and jQuery(this).attr('name')+i;, but it does not work as I intend. infact, it does not work at all. :-)

How can I modify the attr() of the field while is theory it does not exists yet ?

As per comments: fiddle is here: http://jsfiddle.net/obmerk99/uZuWA/

1
  • 1
    Instead of using .html(), use .clone(), and you can set attributes with .attr() as youve been trying Commented May 4, 2013 at 9:00

1 Answer 1

4

The correct use of setting attribute is

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

You should know, that the better use is with clone(), as Ian suggested!

Demo with .clone

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

Sign up to request clarification or add additional context in comments.

8 Comments

You are not setting attribute here but getting instead
You are right, I edited it ... missed, a comma instead of "+"
but using CLONE makes the script works only ONCE . aand also, It does not seem to change the ID´s .. Is there anything else wrong there ?
ok, I understand it is working as a general rule, but on my script it does not. So I figure I have done something else wrong there..
This seems like another question. Can you recreate your problem code in jsbin / jsfiddle?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.