Reputation: 110277
To get all <td>
elements text that starts with "ELEC...", I am doing -
$('td.id').each(function(){
if ($(this).text().indexOf('ELEC') == 0) {}
});
Is there a simpler way to do this, something like $('td.id:contains("ELEC*")')
?
Upvotes: 0
Views: 1924
Reputation: 707546
It seems that if we combine the best of several different proposals, we'd get something faster since a regex is not really called for here:
$("td.id").filter(function() {
return ($(this).text().substr(0, 4) == "Elec");
}).whateverMethodYouWant();
Or a bit faster, that uses less jQuery:
$("td.id").filter(function() {
return ((this.textContent || this.innerText).substr(0, 4) == "Elec");
}).whateverMethodYouWant();
Upvotes: 0
Reputation: 95027
To get only elements that start with ELEC, use the .filter
method.
$("td.id").filter(function(){
return /^ELEC/.test($(this).text());
});
or the slightly more efficient
var $collection = $("td.id");
$collection.filter(function(i){
return /^ELEC/.test($collection.eq(i).text());
});
Upvotes: 5
Reputation: 25694
It looks like that's exactly how you do it (I removed the wildcard asterisk, as its not needed.):
$('td.id:contains("ELEC")')
http://api.jquery.com/contains-selector/
Upvotes: 4