jQuery check if element is in a html table

By Sam Deering

This code snippet recursively searches up the DOM of the parent elements of the input element to find a table element.

//filter parents by HTML table tag
$('.item :first :input[name="code"]').parents('table')

//conditional, not found parent table element
($('.item :first :input[name="code"]').parents('table').length == 0)

Length passes back 1 if found, 0 if not found.

No Reader comments

Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.