Add class (odd and even) in html via javascript? -


i have code example:

<section class="timeline">     <article class="post ">     <article class="post ">     <article class="post ">     <article class="post "> </section> 

now want know how add class via javascript article element. example:

1st article add class "left"

2nd article add class "right"

3rd article add class "left"

4th article add class "right"

i'm not sure want don't need have javascript can write styles odd , childrens.

.post:nth-child(odd) {      color: green;  }  .post:nth-child(even) {      color: red;  }
<section class="timeline">  <article class="post ">article</article>  <article class="post ">article</article>  <article class="post ">article</article>  <article class="post ">article</article>  </section>


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -