Find all text fields where id does not contain specific characters

I have the following code and am trying to extract txtboxes where id does not contain 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 the id starts with txt , but it gives me everything (obviously). How to get only text fields where id starts with txt but does not contain OT ?

 .find("input[id ^= 'txt']") 
+4
source share
2 answers

Do not use selector: use filter :

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

The filter callback should return true if the item should be saved, and false if it should be deleted. In this case, the function will return true , and the element will be saved if indexOf returns -1 , which means that there is no OT .

+6
source

Why don't you use a class for these specific text fields and use the class selector instead?

But still, if you need a solution here.

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

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

Mark

 <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(); 
+2
source

Source: https://habr.com/ru/post/1395621/


All Articles