Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get elements that do not have a certain sibling?

I am trying to use jQuery to find text areas that do not have a sibling of a certain class. Currently I am using the following:

$("textarea").not($("textarea").siblings(".siblingClass").siblings("textarea"))

Is there an alternative?

Sorry for the lack of context. The problem I am having is that I have a jquery plugin (maxlength) that runs multiple times (due to dynamic changes) and adds a div after textareas. The plugin does not check if it has already been run, so it will add the div multiple times.

like image 333
enifeder Avatar asked Jul 07 '15 01:07

enifeder


1 Answers

If I understand the question correctly, .filter() can be useful.

$('textarea').filter(function(){
  return $(this).siblings('.that-class').length == 0;
});

$('textarea').filter(function(){
  return $(this).siblings('.a').length == 0;
}).css( "color", "red" );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div>
    <div class="a"></div>
    <textarea>one</textarea>
    <div class="b"></div>
  </div>
  <div>
    <div class="c"></div>
    <textarea>two</textarea>
    <div class="c"></div>
  </div>
  <div>
    <textarea>three</textarea>
  </div>
  <div>
    <div class="a"></div>
    <textarea>four</textarea>
  </div>
</div>
like image 191
Dhiraj Avatar answered Oct 05 '22 23:10

Dhiraj