1

I have these two arrays:

var numbers = ['one',  'two', 'three'];
var colors  = ['blue', 'red', 'white'];

Now I want this output:

$('.classname').html("<span id = 'one'>blue</span><span id = 'two'>red</span><span id = 'three'>white</span>");

How can I do that?


I can use join and mix second array with <span> but I cannot add id={arr1-values}:

$('.classname').html('<span>' + colors.join('</span><span>') + '</span>');
4
  • 1
    If you want to have a connection between the two arrays, better make them an object, or a Map Commented Feb 21, 2016 at 19:04
  • 1
    I'm a bit late to the party, but here's the jQuery'ish way -> jsfiddle.net/nf2jpnej Commented Feb 21, 2016 at 19:22
  • @adeneo Do you know a function like appendChild() but not append, I want replace, like html() ? Commented Feb 21, 2016 at 19:32
  • 1
    So why not just use html(), it's one of the few jQuery methods that replaces the content. Commented Feb 21, 2016 at 19:36

5 Answers 5

5

If you want to avoid HTML injection, don't concatenate strings like that. Use DOM methods instead:

var numbers = ['one',  'two', 'three'],
    colors = ['red', 'green', 'blue'],
    target = document.querySelector('.classname');
target.innerHTML = ''; // Remove previous contents
numbers.forEach(function(id, i) {
  var el = document.createElement('span');
  el.id = id;
  el.textContent = colors[i];
  target.appendChild(el);
});
#one { color: red }
#two { color: green }
#three { color: blue }
<div class="classname"></div>

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

2 Comments

Out of curiosity, how exactly does this "avoid HTML injection" ?
This has a small problem ... My approach in the question uses .html but your approach always appends, I don't want that, I want to replace new html with current html ..
1

Try to build HTML by iterating arrays:

var numbers = ['one',  'two', 'three'];
var colors  = ['blue', 'red', 'white'];
var resultHTML = '';
for(var i = 0; i < numbers.length; i++){
  resultHTML += '<span id = "'+ numbers[i] + '">' + colors[i] + '</span>'
}

$('.classname').html(resultHTML);

4 Comments

As soon as one of the arrays has a different length, it'll fail
@mmm the question was quite different
@mmm In the most of cases the length of arrays are the same, but how to keep it safe if they weren't?
@mmm the nature of the question implies that the arrays should be the same length
1

This is what you need:

var numbers = ['one',  'two', 'three'];
var colors  = ['blue', 'red', 'white'];

$.each(numbers, function(k, number) {
  $('.classname').append("<span id='"+ number +"'>"+ colors[k] +"</span>");
});

https://jsbin.com/tobozuliya/edit?html,js,output

2 Comments

I don't want .append(), please write html() instead
simply keep what I have inside append function in a variable with += and outside each, $('.classname').html(the variable that you have inside each);
0

Assuming colors and numbers are of same length, one liner.

numbers.map(function(elm,index){
  return '<span id='+elm+'>'+colors[index]+'</span>'
}).join();

As suggested by @mmm it would be better to have in object like

{
 number:'one'
 color:'blue'
}

Comments

-1

It's better if you could use an object to make the relation between the two arrays e.g:

var colors = {one: 'blue', two: 'red', three: 'white'}

Then you can loop through the object and append the spans to the element :

$.each(colors, function(index,value){
    $('.classname').append("<span id="+index+">"+value+"</span>");
})

Hope this helps.


var colors = {one: 'blue', two: 'red', three: 'white'}

$.each(colors, function(index,value){
    $('.classname').append("<span id="+index+">"+value+"</span>");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="classname"></div>

6 Comments

Objects have no order, though. Since OP uses arrays, I guess order is important.
@Oriol The OP don't refer to the Order.
But I used one, two .. to show the ordering is important for me.. Sorry if I didn't mention it literally ..
Okay @stack that make since .. you should mention such informations in the OP.
@ZakariaAcharki don't blame OP...it's pretty clear that html output matches array order
|

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.