2

I have an html template which, on click on a button, I clone and append to a div. I want to change the class name of a div which is nested inside the template, however, I do not know how. I tried looking it up but I couldn't find any way to do that in JavaScript, the only posts I found were about Visual Studio Code etc, however, as mentioned, I want to do this in JavaScript.

The template look something like this:

<template id="newElement">
    <div class="wrapper">
        <p>Test Paragraph</p>
    </div>
</template>

This is how I clone and append the template:

function addElement() {

    var listElement = document.getElementById('newElement');
    var cloneTemplate = listElement.content.cloneNode(true);
    var list = document.getElementById("list");
    list.appendChild(cloneTemplate);
    cloneTemplate.removeAttribute("id");

}

Now, I want to change the class of the newly generated element. I tried this using:

cloneTemplate.content.className = "newName";
cloneTemplate.className = "newName";

I also tried accessing the div inside the template and changing its class, however, I keep getting the error message that the element I try to access is undefined. Does anyone know how I could fix this?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
A.S.J
  • 627
  • 3
  • 14
  • 38
  • You need to learn about scope. – jmargolisvt Nov 20 '17 at 13:59
  • https://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript –  Nov 20 '17 at 13:59
  • What do you mean? I'm trying to change the class name inside the addElement() function so it shouldn't be a problem or am I being slow here? – A.S.J Nov 20 '17 at 14:01
  • @SennerP this is not really relevant as I know how to change a class name generally, the problem is that I'm having trouble accessing the element whose name I want to change – A.S.J Nov 20 '17 at 14:04

1 Answers1

2

If you log the content of var cloneTemplate you will receive something like this: DocumentFragment [ #text, div.wrapper, #text ]

Selecting the div.wrapper by var div = cloneTemplate.querySelector( 'div.wrapper' ); and changing its class to div.className = 'newName'; will result in your desired output.

Put together in your function:

function addElement() {
    var listElement = document.getElementById('newElement');
    var cloneTemplate = listElement.content.cloneNode(true);

    var div = cloneTemplate.querySelector( 'div.wrapper' );
    div.className = 'newName';

    var list = document.getElementById("list");
    list.appendChild(cloneTemplate);
}

Removed cloneTemplate.removeAttribute("id"); because it throws an error and your cloned content does not contain any id's.

DKSan
  • 4,187
  • 3
  • 25
  • 35
  • Thank you very much for your reply, it worked perfectly! I have a question though, earlier, when I went through different stuff to try to change the class name, I tried using querySelectorAll where you used querySelector, however, it didn't work. Do you know why that is (I hope that doesn't stray too far from my initial question) – A.S.J Nov 20 '17 at 14:33
  • 1
    querySelectorAll will return a NodeList ( https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll ) whereas querySelector returns the first element ( https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector ) – DKSan Nov 20 '17 at 14:43