D
D
Demon2015-10-11 15:26:40
JavaScript
Demon, 2015-10-11 15:26:40

How to change div classes?

How can I change the class of a div through one div.
Example:

<div id="test">
<div id="id1" class="1"></div>
<div id="bla-bla" class="propusk"></div>
<div id="id1" class="2"></div>

<div id="id1" class="1"></div>
<div id="bla-bla" class="propusk"></div>
<div id="id1" class="2"></div>
</div>

Option to change the class of divs 1 and 2 but it sets 2 divs - id="bla-bla" class="propusk"
Those. The first div should be given the class left
. The second div should be skipped.
The third div needs to be given the right class
, and so on for all the divs.
$('#id1:even').addClass('left');
$('#id1:odd').addClass('right');

How can I alternate class change by skipping div - id="bla-bla" class="propusk"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-10-11
@In4in

Your code and the enemy will not wish.

$("#test > div").not(".propusk").each(function(i){
   $(this).addClass(i%2 ? "right" : "left");
});

jsfiddle.net/xd1LvLrt/1

S
sanex3339, 2015-10-11
@sanex3339

Do you even understand that multiple elements cannot have the same ID?
Remember - one unique ID for one element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question