www.webdeveloper.com
Results 1 to 3 of 3

Thread: problem with names - loop

  1. #1
    Join Date
    Jul 2010
    Posts
    15

    problem with names - loop

    hi

    i am using this script
    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);
            
            $('#valores').prop('id','valores' + num);
            $('#input' + num).after(newElem);
        }
        });
    </script>
    
    <div id="input1" class="clonedInput">
        <ol id="valores">
            <li>lowest</li>
            <li>highest</li>
        </ol>
    <input readonly id="sliderProgInput" size="20"  name="myformdata[valor][]"/>
    </div>

    all works well, less one thing. The id names of valores are incorrect in the last loop.

    <ol id="valores1">
    <ol id="valores2">
    <ol id="valores3">
    <ol id="valores4">
    <ol id="valores"> //must be valores5

    how can i solve this?

    demo
    http://jsfiddle.net/35eFz/
    Last edited by Kor; 10-12-2011 at 05:47 AM. Reason: add the JQuery prefix

  2. #2
    Join Date
    Sep 2009
    Posts
    159
    I don't really understand why you turned a number into an object (in this case, num+1 into newNum), but don't you need to use valueOf() method to retrieve the actual number inside the .prop(), which basically puts id on the element?
    Got jQuery problem? jQuery vault can help you find solutions. Give it a try and find out.

    Click jQuery vault to help us help you.

  3. #3
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    you cou use .each like so:

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
            $('.clonedInput').each(function(i){
            var newElem = $('#input' + i).clone(true).prop('id', 'input' + i);
            
            $('#valores').prop('id','valores' + i);
            $('#input' + i).after(newElem);
        })
        });
    </script>
    also to answer your question change the 4 in your for loop to a 5

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles