Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all textboxes where id does not contain certain characters

I have the following code and I'm trying to retrieve the txtboxes where id does not contain the the word OT.

<input type="text" id="txtMo1">
<input type="text" id="txtOTMo1">
<input type="text" id="txtTu1">
<input type="text" id="txtOTTu1">
...
...
<input type="text" id="txtMo4">
<input type="text" id="txtOTMo4">
<input type="text" id="txtTu4">
<input type="text" id="txtOTTu4">
...
...

I tried using .find where id starts with txt but it gives me everything (obviously). How do I retrieve only the textboxes where id starts with txt but does not contain OT?

.find("input[id ^= 'txt']")
like image 360
tempid Avatar asked Feb 22 '23 13:02

tempid


2 Answers

Don't use a selector: use filter:

.find('input[id^="txt"]').filter(function() {
    return -1 === this.id.indexOf('OT');
});

The callback to filter should return true if the element should be kept and false if it should be removed. In this case, the function will return true and the element will be kept if indexOf returns -1, which means that OT is not present.

like image 76
lonesomeday Avatar answered May 02 '23 18:05

lonesomeday


Why don't you use a class instead for those specific textboxes and use a class selector instead?

But still if you need a solution here it is.

$('input[id^-"txt"]').filter(function() {
    return this.id.indexOf('OT') == -1;
});

You can render your mark something like this and make your code simple.

Mark up

<input type="text" id="txtMo1">
<input type="text" class="OT" id="txtOTMo1">
<input type="text" id="txtTu1">
<input type="text" class="OT"id="txtOTTu1">

Js

$('input.OT').doYourStuff();
like image 29
ShankarSangoli Avatar answered May 02 '23 18:05

ShankarSangoli