Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript DOM manipulation with odd even class

Tags:

I have HTML like this

<div class="oddeven">
    <p id="p1" class="odd">Lorem ipsum</p>
    <p id="p2" class="even">dolor sit amet</p>
    <p id="p3" class="odd">consectetur adipiscing</p>
    <p id="p4" class="even">sed do</p>
    <p id="p5" class="odd">eiusmod tempor</p>
    <p id="p6" class="even">incididunt ut</p>
</div>
<button class="btnClick">Click</button>

I want to show only two paragraph like this

<div class="oddeven">
    <p id="p1" class="odd active">Lorem ipsum</p> // every 'odd' class will show here
    <p id="p2" class="even active">dolor sit amet</p> // every 'even' class will show here
</div>
<button class="btnClick">Click</button>

The rule is "Starting from '#p1', only one paragraph will change on click button, from odd to even, Odd class will change to another odd class, and even class will change to another even class".

Example first change will look like this (first button click)

<div class="oddeven">
    <p id="p3" class="odd active">consectetur adipiscing</p> // #p1 change to #p3
    <p id="p2" class="even active">dolor sit amet</p>
</div>

Example second change (second button click)

<div class="oddeven">
    <p id="p3" class="odd active">consectetur adipiscing</p>
    <p id="p4" class="even active">sed do</p> // #p2 change to #p4
</div>

Next button click will change odd, then even, odd, even, and so on.. Anyone please help me, I would highly appreciate..

$(document).ready(function(){
  var first_odd = $('.oddeven').children('.odd')[0];
  var first_even = $('.oddeven').children('.even')[0];

  $(first_odd).addClass('active');
  $(first_even).addClass('active');

  var odd_sibs = $(first_odd).siblings('.odd');
  var even_sibs = $(first_even).siblings('.even');

  $('.btnClick').on('click', function(){
    // I don't know what to do
  })
})
.odd {
	color: red;
}
.even {
  color: blue;
}
.oddeven p {
  display: none;
}
.active {
  display: block!important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="oddeven">
	<p id="p1" class="odd">Lorem ipsum</p>
	<p id="p2" class="even">dolor sit amet</p>
	<p id="p3" class="odd">consectetur adipiscing</p>
	<p id="p4" class="even">sed do</p>
	<p id="p5" class="odd">eiusmod tempor</p>
	<p id="p6" class="even">incididunt ut</p>
</div>
<button class="btnClick">Click</button>
like image 921
fandiahm Avatar asked Apr 05 '17 03:04

fandiahm


People also ask

Can DOM be manipulated using JavaScript?

In website development, DOM stands for Document Object Model. It is a programming interface that allows us to create, change, or remove elements from a website document. DOM manipulation is when you use JavaScript to add, remove, and modify elements of a website. It is very common in web development.

Which method is used for DOM manipulation?

The jQuery after() method inserts content (new or existing DOM elements) after target element(s) which is specified by a selector. Syntax: $('selector expression').

Is DOM manipulation synchronous?

Other DOM manipulations (inserting elements, removing them, moving them) are also synchronous, although the user may not see the result until your JavaScript code completes, letting the browser use the thread to repaint the display.

Does DOM manipulation allow manipulating styles of elements?

DOM manipulation is interacting with the DOM API to change/modify the HTML document that is to be rendered on the web browser. This HTML document can be changed/modified to add elements, remove elements, edit elements, move elements around, etc.


1 Answers

Here's one way to do it:

var odd = $(".odd")            // save a reference to the list of odd
var even = $(".even")          // and the list of even elements

odd.eq(0).addClass("active")   // display the first odd
even.eq(0).addClass("active")  // and first even

odd.prependTo(".oddeven")      // move the odd ones in front of the even
                               // so that when visible they'll always be
                               // before the even

var current = 0                // index of item currently shown
var next = odd                 // type to show next

$("button.btnClick").on("click", function() {
  if (next === odd)                             // if next is odd 
    current = (current + 1) % odd.length        // go to next index

  next.filter(".active").removeClass("active")  // deactivate previous one
  next.eq(current).addClass("active")           // activate next
  
  next = next === odd ? even : odd              // set which type to do next
})
.odd { color: red; }
.even { color: blue; }
.oddeven p { display: none; }
.active { display: block!important; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="oddeven">
    <p id="p1" class="odd">p1 - Lorem ipsum</p>
    <p id="p2" class="even">p2 - dolor sit amet</p>
    <p id="p3" class="odd">p3 - consectetur adipiscing</p>
    <p id="p4" class="even">p4 - sed do</p>
    <p id="p5" class="odd">p5 - eiusmod tempor</p>
    <p id="p6" class="even">p6 - incididunt ut</p>
</div>
<button class="btnClick">Click</button>
like image 117
nnnnnn Avatar answered Sep 22 '22 11:09

nnnnnn