JavaScript How to add Class in javascript using button class javascript use strict,javascript class using function,javascript class

I want to trying adding class using javascript, I know there is a lot of topic about this in stackoverflow but some of them is too complicated for me to understand, let say I have this simple code

this is some topic that I already read:

addclass-to-getelementsbyclassname-array

add-css-class-using-document-getelementsbyclassname

add-class-using-getelementsbyclassname-javascript

this is my html

<p>
    test 1
  </p>
  <h2 class="test-2">
    test 2
  </h2>
  <h3 class="test-3">
    test 2
  </h3>

  <button onClick="addClass">
    click me
  </button>

this is my css:

p{
  color: red;
}

.test-2{
  font-size: 2em;
  color: blue;
}

.test-3{
  font-size: 5em;
  font-weight: bold;
}

and this is my js:

function addClass () {
    var x = document.getElementsByClassName("test-3")[0]; 
 return  x[0].className += ' test-2';
}

where did I do it wrong? I'm quite confused since I'm new in javascript

Answer:1

getElementsByClassName returns a live collection of elements. Grabbing the first index [0] is correct, but you don't need to do it a second time:

x.className += ' test-2'

You may find using querySelector and classList a little easier as their interfaces are newer and often more suitable for modern JS development.

querySelector allows you to use CSS selectors to select elements, and returns the first instance of an element found with that selector. (Its sister method querySelectorAll returns a (non-live) nodelist which you can iterate over).

classList allows you to simply add and remove classes from an element without the need to concatenate class strings to each other.

Here's a demo.

const button = document.querySelector('button');
button.addEventListener('click', addClass, false);

function addClass() {
  var x = document.querySelector('.test-3');
  x.classList.add('test-2');
}
p { color: red; }
.test-2 { font-size: 2em; color: blue; }
.test-3 { font-size: 5em; font-weight: bold; }
<p>test 1</p>
<h2 class="test-2">test 2</h2>
<h3 class="test-3">test 2</h3>
<button>click me</button>
Answer:2

I have two variables with JSON files. The first is a list of keys looks like this: keylist = ["key1","key2","key3"] The second one is generated from a database and looks like this: data = { "...

I have two variables with JSON files. The first is a list of keys looks like this: keylist = ["key1","key2","key3"] The second one is generated from a database and looks like this: data = { "...

  1. access array with pointer c++
  2. access array with pointer
  3. access array with key javascript
  4. access array with index php
  5. access array with python
  6. access array of objects javascript
  7. access array in javascript
  8. access array in php
  9. access array of objects
  10. access array in matlab
  11. access array from another class java
  12. access array of json objects javascript
  13. access array in jquery
  14. access array in json
  15. access array by index javascript
  16. access array in c
  17. access array in dictionary python
  18. access array in struct c
  19. access array in java
  20. access array in scala

I have an array like below and I want to extract objects and sub-array and create a new array if the selected object value is true menu: [ { category_name: "snacks", selected: true ...

I have an array like below and I want to extract objects and sub-array and create a new array if the selected object value is true menu: [ { category_name: "snacks", selected: true ...

I have included javascript for dynamically adding input fields but the first time its clicked it doesn't style correctly. Here is how it looks before utilizing add button (ignore the Author 1/...

I have included javascript for dynamically adding input fields but the first time its clicked it doesn't style correctly. Here is how it looks before utilizing add button (ignore the Author 1/...

  1. dynamic adding new input field
  2. dynamic adding new input field ionic

I have been struggling to include a profile picture upload along with regular text data and send that all to the back end to create a new user through mongoose. I have tried everything from ng-file-...

I have been struggling to include a profile picture upload along with regular text data and send that all to the back end to create a new user through mongoose. I have tried everything from ng-file-...

  1. angular image upload with preview
  2. angular image upload component
  3. angular image upload npm
  4. angular image upload example
  5. angular image upload stackblitz
  6. angular image upload with crop
  7. angular image upload base64
  8. angular image upload library
  9. angular image upload validation
  10. angular image upload plugin
  11. angular image upload made easy
  12. angular image upload with spring boot
  13. angular image upload with preview example
  14. angular image upload firebase
  15. angular image upload .net core
  16. angular image upload and display
  17. angular image upload and retrieve with firebase storage
  18. angular image upload formdata
  19. angular image upload drag and drop
  20. angular image upload with form data