JavaScript
Article

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

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

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