0

I'm creating buttons dynamically from a javascript for loop by adding this code.

var container = document.getElementById('myList');
var new_element = document.createElement('li');
new_element.innerHTML = '<button type="button" id="">label</button>';

I need to add elements from an array to both the buttons id and label. Like this for example;

arrMarkers[i].title

How can I enclose these array values in the string that creates the button?

2
  • whats the issue? its a usual concate '<button type="button" id="'+arrMarkers[i].title+'">label</button>'; Commented Mar 12, 2013 at 10:48
  • Did you get button over there...?? As far i know , you wouldn't get any button Commented Mar 12, 2013 at 10:55

2 Answers 2

1
newElement.innerHTML = '<button type="button" id="' + arrMarkers[i].id + '">' + arrMarkers[i].title + '</button>';
Sign up to request clarification or add additional context in comments.

Comments

1

As I understood, you are looking for string concatenation:

new_element.innerHTML = '<button type="button" id="' + arrMarkers[i].id + '">' + arrMarkers[i].title + '</button>';

4 Comments

:D Who didn't like string concatenation?
can't get why this answer is downvoted. can someone explain this to me?
may be its too simple for Vision to Answer...:). Why not upvote if it deserve one. and make the downvote meaningless
@RabNawaz Ha! Sorry for easy answer :) You are very welcome to even the overall score ;)

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.