0

I have the following code: http://jsfiddle.net/stomp/a8vjg/

Everything works well except I need the name of the select field to be extras[0], then extras[1] for the next one added and so on..

Can anybody help?

Cheers, Steve

1
  • 1
    In future, please don't just include a link to jsFiddle. Your post should standalone from any other resource; consider what'd happen if jsFiddle went down in the future. Commented May 15, 2012 at 12:25

3 Answers 3

1

First you should name your select element with "extras" only ...

<select name="extras">

Then get it using

elemenets = $('[name=extras]')

it will be an Array you can access by extras[0] , extras[1] , ..... etc

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

Comments

0

Do you mean:

case "add":
                var newTag = tag.clone();
                var i = $("#multi").find("select").length;
                console.log(i);
                 newTag.find("select").attr("name", 'extras['+i+']');              
            tag.after(newTag.find("input").val("").end());
            break;

See: jsFiddle

1 Comment

This sometimes creates duplicates if you add/remove items a lot
0

You need sth like this when cloning:

case "add":
            var cnt=jqEl.parent().siblings().size();
            var cloned=tag.clone();
               $(cloned).find("input").val("")
                   .end()
                   .find('select').attr('name','extra['+cnt+']');

            tag.after(cloned);
            break;

check the working fork (might be duplicates if you remove and add)

No duplicates fiddle (serial number could not be ensured if you delete a node)

So the perfect solution could be adding the ids on [Save] button here is the solution

function save(e) {
        multiTags.find("select").each(function(i){
            $(this).attr('name','extras['+i+']');
        });

        var tags = multiTags.find("input.tag").map(function() {
            return $(this).val();
        }).get().join(',');
        alert(tags);
        return false;
    }

2 Comments

This sometimes creates duplicates if you add/remove items a lot
what you want when a node is removed , reset the names ?

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.