I’m trying to make an if-statement where if the button’s parent is an even child of it’s parent, do alert("it works");, else do alert("nope");, it’s probably something really easy, I’m really new to this! 🙂 Here’s my current code:
HTML:
<div>
<div>
<button>#0</button>
</div>
<div>
<button>#1</button>
</div>
<div>
<button>#2</button>
</div>
<div>
<button>#3</button>
</div>
</div>
JS:
$(function () {
$("button").click(function () {
var $parent = $(this).parent();
if ($parent.is(":even")) {
alert("it works");
} else {
alert("nope");
}
});
});
jsFiddle: http://jsfiddle.net/goldson/Rn6gz/1/
Thanks in advance!
You can use index() to get the index and use modulos operator
%to know if it iseventorodd.Live Demo