Tony
Tony

Reputation: 12695

Check an element existence in table rows via jQuery

I have a table tag on my page. I'm adding dynamically a new set of rows, where some of them can contain a <button class="btn btn-primary"></button> tag. I'm wondering, how to check the information via JQuery, if that table contains any rows with that button ?

Upvotes: 0

Views: 837

Answers (3)

user818991
user818991

Reputation:

if ($('tr:contains(".btn")')){
  alert('has button')
}

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use length to check the existence of an element. Try this:

var buttonExists = $("table .btn-primary").length != 0;

Upvotes: 2

DerWaldschrat
DerWaldschrat

Reputation: 1915

Just try:

var buttons = $table.find('.btn.btn-primary');
var buttonExists = buttons.length > 0;

Table has to be a jQuery object containing your table.

Upvotes: 3

Related Questions