0

I have a function:

jQuery(function($) {
var scntDiv = $('#textfields');
var i = $('#textfields p').size() + 2;
var a = 0;
$('#addbutton').click (function() {
$('<p><input type="text" id="test_testbutton" name="test_testbutton[a][0]" value="" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton[a][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>').appendTo(scntDiv);
                i++;
        a++;
                return false;
        });

$('#removebutton').live('click',function() { 
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});

And what I'd like to achieve is to make 'test_testbutton[a][0]' a 'test_testbutton[0][0], test_testbutton[1][0], test_testbutton[2][0]...etc' with every iteration. You may laugh, but I'm seraching for any info about it since today morning and I couldn't find the exact example. When I "console.log" the variable everything works perfectly, but I don't know how to put a variable inside HTML...

1

1 Answer 1

1

Just concatenate the value into the string like so:

'<p><input type="text" id="test_testbutton" name="test_testbutton['+a+'][0]" value="" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton['+a+'][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>'

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

1 Comment

This is IT!! i tried to use '+a' but it gave me an error every time I used it, I wasn't aware that it's needed to add another + after a variable. Thank you!!

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.