I have a table and I want to know if its last td its id contains a certain string. For example, if my last td has id "1234abc", I want to know if this id contains "34a". And I need to do that in a 'if' statement.
if(myLastTdId Contains "blablabla"){ do something }
Thanks!!!
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.
The #id selector selects the element with the specific id. The id refers to the id attribute of an HTML element. Note: The id attribute must be unique within a document. Note: Do not start an id attribute with a number. It may cause problems in some browsers.
You could use the "attributeContains" selector:
if($("#yourTable td:last-child[id*='34a']").length > 0) {
//Exists, do something...
}
This is easily done with indexOf
and last-child
.
<table id='mytable'>
<tr>
<td id='abc'></td>
<td id='cde'></td>
</tr>
</table>
<script>
if($('#mytable td:last-child').attr('id').indexOf('d') != -1) {
alert('found!');
}
</script>
Here it would alert 'found' because d
appears in the string cde
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With