0

What is the proper syntax for creating multiple HTML elements with jQuery when using the JSON format? For example...

inputs = $("<select/>", {
    html: list,
    id: 'select'+siteNumber+''+x+'',
    name: 'select'+siteNumber+'['+x+']'
    });

creates a select input, but I have about 6 other input fields (of different types, not just submit) that I want to store in the inputs variable. How can I put more than one element into the variable?

Thanks very much.

1
  • You may want to read this question and its answers... not a dupe, but you might find it helpful. Commented Apr 1, 2016 at 12:11

1 Answer 1

0

Use some loop and push your new elements in array.

var inputs = [];
for (var i = 0; i<6; i++) {
    inputs.push($("<select/>", {
        html: list,
        id: 'select'+siteNumber+''+x+'',
        name: 'select'+siteNumber+'['+x+']'
    }));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I should have been more specific - they are not all select elements. Regardless, this may do the trick...

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.