var cName = ($(".myclass").next().attr('class'));
alert(cName);
if (cName == "c1") {
$("#Output").addClass("green");
$("#Output").removeClass("red");
} else {
$("#Output").addClass("red");
$("#Output").removeClass("green");
}
<p class="myclass">If you click on me, I will disappear.
<span class="c1"></span>
<span class="c2"></span>
<span class="c3"></span>
<span class="c4"></span>
<div class="d1"></div>
<div class="d2"></div>
<div id="Output" style="width:25px; height:25px;"></div>
</p>
I am trying to get the .classname
of the first span
element. Why is jquery not fetching the next span
element .classname
? Please help.
` so `.next()` wont work. Use `$(".myclass span:first")`
– Shaunak D Jun 25 '15 at 05:30