[RESOLVED]how to find matching result of an element plus its decedents?

ie. if given a DOM element, I want to check all this decedents which are <input type="checkbox" />, however, if the DOM element itself is <input type="checkbox" /> I want to return itself.

$(element).find(‘input[type=checkbox]‘) will return only matching decedents, not element itself.

Is there a way to do this instead of writing if-else statement?

simple:

$(element).find(‘input[type=checkbox]‘).addBack(‘input[type=checkbox]‘);

Hi Cheng Bao,

Thanks for your post.

As for your problem, I agree with bruce. What I’d like to add is that we could use .addBack() method to add  the previous set of DOM elements in the traversal stack to the current set. For more information about
.addBack() method, you could refer to the following link.

If you have any question about my reply, please let me know freely.

Best Regards,

Fei Han

Leave a Reply