How to add html class to above element

Adam Dofe

I hope this title says what I want to do. I have a checkbox on my site. I want to check variable and then add class='checked' to above element. For example:

<div class="checker" id="uniform-personalData1"><span><input name="personalData1" id="personalData1" value="0" type="checkbox"></span></div>

If js variable like say $a = 1 I want to add class='checked' to <span>. Like this:

<div class="checker" id="uniform-personalData1"><span class="checked"><input name="personalData1" id="personalData1" value="0" type="checkbox"></span></div>

How can I do this thing by using jquery ?

Kind regards

SM Farhad Ali

Hi @adam It's quite simple. Just use addClass method of jQuery. Like below.

    var $a = 1;
    if ($a === 1) {
      $('#uniform-personalData1 > span').addClass('checked');
      console.log($('#uniform-personalData1 span'));
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to add "class" to host element?

From Java

How do I add a class to a given element?

From Dev

How to add CSS class to the HTML5 picture element

From Dev

How to add class to html element with Deface?

From Dev

How to make html text rise above absolutely positioned element?

From Dev

Add class to an element when user scrolls to 50px above it?

From Dev

How to add the `active` class on click of `li` element

From Dev

Add a calculated class to an HTML element in angular

From Dev

How to add CSS class dynamically to html element

From Dev

How to add a class to an element with CSS

From Dev

How to add a class to parent element when there are several

From Dev

OM how to add class to html element

From Dev

how to add class to element from its id?

From Dev

How to get an element above $ionicBackdrop?

From Dev

How to add a class to html tag?

From Dev

How to add class to the third <li> element in javascript

From Dev

How to add a class to a given element?

From Dev

JQUERY How to add class with text inside element

From Dev

How to add an element to a null Set<class>?

From Dev

How to add class to html element in django-bootstrap3 template

From Dev

How to add a class to an element when another element gets a class in angular?

From Dev

How to get an element above $ionicBackdrop?

From Dev

How to add a class to element if another element is empty?

From Dev

How to force an element above another

From Dev

How to add a "modified" class for an element in SCSS

From Dev

How to add | remove css class in angular element?

From Dev

how to add to an HTML time element

From Dev

How add a class to a label element with Symfony forms?

From Dev

How to add HTML class based on the contents of another element?

Related Related

  1. 1

    How to add "class" to host element?

  2. 2

    How do I add a class to a given element?

  3. 3

    How to add CSS class to the HTML5 picture element

  4. 4

    How to add class to html element with Deface?

  5. 5

    How to make html text rise above absolutely positioned element?

  6. 6

    Add class to an element when user scrolls to 50px above it?

  7. 7

    How to add the `active` class on click of `li` element

  8. 8

    Add a calculated class to an HTML element in angular

  9. 9

    How to add CSS class dynamically to html element

  10. 10

    How to add a class to an element with CSS

  11. 11

    How to add a class to parent element when there are several

  12. 12

    OM how to add class to html element

  13. 13

    how to add class to element from its id?

  14. 14

    How to get an element above $ionicBackdrop?

  15. 15

    How to add a class to html tag?

  16. 16

    How to add class to the third <li> element in javascript

  17. 17

    How to add a class to a given element?

  18. 18

    JQUERY How to add class with text inside element

  19. 19

    How to add an element to a null Set<class>?

  20. 20

    How to add class to html element in django-bootstrap3 template

  21. 21

    How to add a class to an element when another element gets a class in angular?

  22. 22

    How to get an element above $ionicBackdrop?

  23. 23

    How to add a class to element if another element is empty?

  24. 24

    How to force an element above another

  25. 25

    How to add a "modified" class for an element in SCSS

  26. 26

    How to add | remove css class in angular element?

  27. 27

    how to add to an HTML time element

  28. 28

    How add a class to a label element with Symfony forms?

  29. 29

    How to add HTML class based on the contents of another element?

HotTag

Archive