Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery find all except

I have following HTML:

<div id="123" class="test">
   <div class="testMessage">Foo</div>
   <div><div class="testDate">2010</div></div>
   <div id="127" class="test">
      <div class="testMessage">Bar</div>
      <div><div class="testDate">2011</div></div>
   </div>
</div>

And I have following JS/jQuery code:

$(".test").find(".testDate").val("cool 2010");

How to change JS/jQuery to find "testDate" class element except in children "test" class block without using children?

P.S. I know only about class name and I don't know how many divs can be nested.

like image 906
Mirgorod Avatar asked Jun 24 '26 00:06

Mirgorod


1 Answers

Update

Its probably the weirdest selector I've ever written:

$("div.test").not(':has(> .test)').siblings().find('.testDate').text('cool 2010');

Demo: http://jsfiddle.net/mrchief/6cbdu/3/

Explanation:

$("div.test")             // finds both the test divs
  .not(':has(> .test)')   // finds the inner test div
  .siblings()             // get all other divs except the inner test div
like image 96
Mrchief Avatar answered Jun 25 '26 14:06

Mrchief



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!