2

I have a custom element, which is created like:

connectedCallback()
{
  var on_off_switch = document.createElement('Div');
  on_off_switch.class = 'switch demo3';

  this.appendChild(on_off_switch);

 }

But I noticed when viewing the source that the on_off_switch's class is not set to switch demo3.

All other assignments work, like on_off_switch.style['background-color'] = 'red', etc.

Is it possible to set the CSS class of a element appended inside a custom element?

0

2 Answers 2

6

For historical reasons, the className property is used instead of class:

function connectedCallback () {
  var on_off_switch = document.createElement('div')
  on_off_switch.className = 'switch demo3'

  this.appendChild(on_off_switch)
}

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

Comments

0

class is the name of the element attribute, while className is its corresponding property:

on_off_switch.className = 'switch demo3'
// or    
on_off_switch.setAttribute('class', 'switch demo3')

Comments

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.