Petru Lebada
Petru Lebada

Reputation: 1682

Get the first next certain element

I try to get the first next div selector , so i can its css , but i can manage to do it,for some reason it returns an object.I may have wrong knowledge about 'next' .

HTML:

<div class="bla" onclick="Find(this.className);"></div>
<p></p>
<div class="first"></div>
<div class="second"></div>

Javascript:

function Find(x){
   $('.'+x).next('div:first').css({'display': 'none'});
   console.log($('.'+x).next('div:first'));
}

Upvotes: -1

Views: 51

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388436

You can't use next as it will search only the next sibling element

function Find(x) {
  $(x).nextAll('div').first().css({
    'display': 'none'
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="bla" onclick="Find(this);">d</div>
<p></p>
<div class="first">f</div>
<div class="second">s</div>


Since you are using jQuery, try to use jQuery event handlers

Upvotes: 1

Related Questions