0

Suppose that I have an array. How can I wrap each element in the array in a span tag? Also, if I want to .appendchild each span element to a div element, how can I do it?

 function add_span(arr){  
     var new_arr= document.createElement("span");// arr is passed in correctly 
     var text=document.createTextNode(arr); // text cannot be output
     new_arr.appendChild(text);
     return new_arr;
 }

and in the main:

 for (i = 0; i < unique_array.length; i++)
 { 
     span_array[i]=add_span(unique_array[i]);
 }
 var cloud_text=document.createTextNode(span_array);
 cloud.appendChild(cloud_text);

the output now looks like this: [object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement],[object HTMLSpanElement]

3
  • 1
    Google: DOM manipulation & append div. Commented Nov 14, 2014 at 17:52
  • This question appears to be off-topic because it does not show the questioner put any effort into answering the question for her/his self. Commented Nov 14, 2014 at 19:06
  • Isn't an array of HTMLSpanElements what you were looking for? Commented Nov 17, 2014 at 5:30

2 Answers 2

2

JSFiddle Example (Updated)

Assume that you have an array of strings:

var arrayExample = ["apple", "orange", "pear"];

Iterate through each string within the array:

arrayExample.forEach(main);

Append the array string item into a span object, and then into a div object:

Edited

function main(arrayItem, index, array) {
    var spanObj = "<span>" + arrayItem + "</span>"
    var divObj = document.createElement('div');
    divObj.innerHTML = spanObj;

    // Do something like:
    document.body.appendChild(divObj);

    // Replace old array string with new array string wrapped in <span>
    arrayExample[index] = spanObj
}
alert(arrayExample); // will output your new Array 
Sign up to request clarification or add additional context in comments.

5 Comments

I am not sure why but I keep getting the <span> tags in my output. function span_c(arr){ arr="<span>"+arr+"</span>"; return arr; } // and in the main : for (i=0;i<unique_array.length;i++){ unique_array[i]=span_c(unique_array[i]); }
It says in your question that you want the output to be wrapped in a span. What is desired return for arr?
more accurately I want to wrap each element in the array in a span
So say you have array = ["1", "2"]. You're trying to turn the array into array = ["<span>1</span>", "<span>2</span>"]?
Have you seen the Fiddle example? Add a debugger; line in the code and update -> You can step through the function and see exactly what each step does.
0

You want to loop through all of the array elements and add span tags to the beginning and end like so:

var example = [];

for(var i = 0; i < example.length; i++) {
    example[i] = '<span>' + example[i] + '</span>';
}

This adds the html span tags to your text and then your array element inside. The i acts as the identifier for which array object you are moifying.

4 Comments

Please add some explanation to your answer. Code-only answers are sometimes good enough, but code+explanation answers are always better
When I do this, it makes it a string, right? then when I display it, is should something like <span> apple </span>. Apparently, I do not want <span> to display.
@Barranka Thanks, I was in a rush but I've updated now.
@YINGL Yes it will make it a string but because <span> is a HTML tag, you won't see it outputted to your screen.

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.