Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't select SPAN sibling

Can't understand why this code changes color of DIV element to blue, but doesn't change color of a SPAN element. Any ideas?

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script>
    $(document).ready(function(){
      $("#prev ~ div").css("color", "blue");
      $("#prev ~ span").css("color", "red");
    });
  </script>
</head>
<body>
  <span id="prev">span#prev</span>
  <div>div sibling</div>
  <span>span sibling</span>
</body>
</html>

Noticed what if I replace

<span id="prev">span#prev</span>

with

<p id="prev">span#prev</p>

both DIV and SPAN change text color.

Thanks!

like image 963
Alexander Prokofyev Avatar asked Apr 17 '26 07:04

Alexander Prokofyev


1 Answers

Looks like you found a bug.

$("#prev ~ span:not(#prev)") works, as does $("#prev").siblings("span").

like image 154
Paul Avatar answered Apr 18 '26 22:04

Paul



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!