V
V
Vetal2018-02-09 15:22:34
css
Vetal, 2018-02-09 15:22:34

How to add a class if the tag has a specific style?

If the h1 tag has styles "text-align:center" then add a class to this tag. Here is an example of code that I am trying to write myself, but it adds a class to all h1 tags on the page

$('h1').each(function (){
        if($('h1').css('text-align') == 'center') {
        	$(this).addClass('centered');
        } else {
        	$(this).removeClass('centered');
    }
  })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stimulate, 2018-02-09
@xarizmat

$('h1').each(function (){
        if($(this).css('text-align') == 'center') {
        	$(this).addClass('centered');
        } else {
        	$(this).removeClass('centered');
    }
  })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question