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 Dev

How to add CSS class dynamically to html element

From Dev

How to add class to html element with Deface?

From Dev

OM how to add class to html element

From Dev

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

From Dev

How to add CSS class to the HTML5 picture element

From Dev

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

From Dev

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

From Dev

How to add a class to an element with CSS

From Java

How to add "class" to host element?

From Dev

How to add a class to a given element?

From Dev

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

From Dev

How to make html text rise above absolutely positioned element?

From Dev

Add a calculated class to an HTML element in angular

From Dev

how to add to an HTML time element

From Dev

How to add a class to html tag?

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Java

How do I add a class to a given element?

From Dev

how to add class to element from its id?

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 | remove css class in angular element?

From Dev

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

From Dev

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

From Dev

How to get an element above $ionicBackdrop?

From Dev

How to get an element above $ionicBackdrop?

From Dev

How to force an element above another

Related Related

  1. 1

    How to add CSS class dynamically to html element

  2. 2

    How to add class to html element with Deface?

  3. 3

    OM how to add class to html element

  4. 4

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

  5. 5

    How to add CSS class to the HTML5 picture element

  6. 6

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

  7. 7

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

  8. 8

    How to add a class to an element with CSS

  9. 9

    How to add "class" to host element?

  10. 10

    How to add a class to a given element?

  11. 11

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

  12. 12

    How to make html text rise above absolutely positioned element?

  13. 13

    Add a calculated class to an HTML element in angular

  14. 14

    how to add to an HTML time element

  15. 15

    How to add a class to html tag?

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

    How do I add a class to a given element?

  21. 21

    how to add class to element from its id?

  22. 22

    JQUERY How to add class with text inside element

  23. 23

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

  24. 24

    How to add | remove css class in angular element?

  25. 25

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

  26. 26

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

  27. 27

    How to get an element above $ionicBackdrop?

  28. 28

    How to get an element above $ionicBackdrop?

  29. 29

    How to force an element above another

HotTag

Archive