A. Appleby A. Appleby - 8 months ago 52
jQuery Question

How can I confirm that an element is the first of its class in jQuery?

I need to know whether the element being clicked on is the first of its kind, because if it is, I need to uncheck a checkbox and re-run a function that prevents inputs being matched; however, something that I thought would be seemingly simple is apparently not that simple. I thought it would be as simple as:

$(".remove-box").unbind().click(function (e) {

if($(this) == $(".remove-box :first")) {
//uncheck tickbox and remove matching details class


Has anyone got any suggestions for how I can make this work?


By "first of its kind" I assume you mean its type, i.e. tag. In which case:

if ($(this).is(':first-of-type)) {

If you mean first of a certain class, it gets more involvd. First of a certain class in the entire document? In the current container? Assuming the latter:

if ($(this).prevAll('.className').length) {