2 Replies - 7249 Views - Last Post: 11 October 2011 - 05:17 PM

#1 felito  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 08-December 10

loop is not working - clone

Posted 11 October 2011 - 04:03 PM

hi

i have this code

<script type="text/javascript">
$(document).ready(function() {
	for ($i=0; $i<4; $i++) {
        var num = $('.clonedInput').length;
        
        var newNum = new Number(num + 1);
        
        var newElem = $('#input' + num).clone(true).prop('id', 'input' + newNum);
        var newElem1 = $('#valores' + num).clone(true).prop('id', 'valores' + newNum);
        

        $('#input' + num).after(newElem);
        $('#valores' + num).insertAfter(newElem1);

    }

    });
</script>

<div id="input1" class="clonedInput">
	<ol id="valores1">
		<li>lowest</li>
		<li>highest</li>
	</ol>
<input readonly id="sliderProgInput" size="20"  name="myformdata[valor][]"/>
</div>



the input id is changed correctly, but valores no. Why ?? Valores is always id="valores1", and must be id="valores1", id="valores2", and so on.

demo
http://jsfiddle.net/Gpugn/

Is This A Good Question/Topic? 0
  • +

Replies To: loop is not working - clone

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: loop is not working - clone

Posted 11 October 2011 - 04:49 PM

Clone() copies an element and all its decedent elements. Meaning when you do a clone of input1, you are also copying valores1. That is why you see a bunch of elements saying valores1. You have to clone input1, alter velores in that clone to say valores2 and then insert it. Otherwise you will just copy the previous input element (along with its sub item valores1) and repeat it.

I hope you get what I am saying here. I copy input1 (which includes valores1), alter its id to say input2 and then insert it as input2 (but with valores1 as its child element... unchanged).

The selector trying to select Valores2 just won't work, valores2 never exists to clone it.

:)

This post has been edited by Martyr2: 11 October 2011 - 04:51 PM

Was This Post Helpful? 1
  • +
  • -

#3 felito  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 08-December 10

Re: loop is not working - clone

Posted 11 October 2011 - 05:17 PM

you are correct, thanks

This post has been edited by felito: 11 October 2011 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1