Lju
Lju

Reputation: 117

Create Table Header with innerHTML

I want to create header of a table with the elements that are of the class "headerClass" but previously remove the duplicates with a JavaScript function. My JavaScript Function:

function getHeaderElements() {

var headerElements = document.getElementsByClassName("headerElement");
var output = document.getElementById("output");

var uniqueHeaderElements = [];
$.each(headerElements, function(i, el){
    if($.inArray(el, uniqueHeaderElements) === -1) uniqueHeaderElements.push(el);
});

output.innerHTML =  uniqueHeaderElements.toString() ;

}

However, I am not sure how I should iterate through the elements of the array and add them to the innerHTML, similar like in the code below:

output.innerHTML =  uniqueHeaderElements.toString() ;

Any help would be much appreciated!

Upvotes: 1

Views: 420

Answers (1)

JBO
JBO

Reputation: 270

you need to do the same loop as you have already done to add all your elements inside the innerHTML.

Try this out :

$.each(uniqueHeaderElements, function(i, el){
      output.innerHTML += el;
 });

Still, i'm not sur of what's inside your table uniqueHeaderElements. You may need to adapt this piece of code a bit.

Upvotes: 1

Related Questions