I'm trying to find all elements on a page whose element ID contains a certain text. I'll then need to filter the found elements based on whether they are hidden or not. Any help is greatly appreciated.
Asked
Active
Viewed 2.4e+01k times
4 Answers
238
$('*[id*=mytext]:visible').each(function() {
$(this).doStuff();
});
Note the asterisk '*' at the beginning of the selector matches all elements.
See the Attribute Contains Selectors, as well as the :visible and :hidden selectors.

karim79
- 339,989
- 67
- 413
- 406
-
20Maybe worth mentioning that when matching against an element's `id` you do not use quotes, where when matching against a `name` you do. `$('*[name*="myname"]:visible')` Not the most intuitive and has caught me up before. – ficuscr Oct 03 '13 at 01:55
-
I replaced $(this).doStuff(); with this.doStuff(); and worked – Carlos López Marí Jan 11 '20 at 18:07
172
If you're finding by Contains then it'll be like this
$("input[id*='DiscountType']").each(function (i, el) {
//It'll be an array of elements
});
If you're finding by Starts With then it'll be like this
$("input[id^='DiscountType']").each(function (i, el) {
//It'll be an array of elements
});
If you're finding by Ends With then it'll be like this
$("input[id$='DiscountType']").each(function (i, el) {
//It'll be an array of elements
});
If you want to select elements which id is not a given string
$("input[id!='DiscountType']").each(function (i, el) {
//It'll be an array of elements
});
If you want to select elements which name contains a given word, delimited by spaces
$("input[name~='DiscountType']").each(function (i, el) {
//It'll be an array of elements
});
If you want to select elements which id is equal to a given string or starting with that string followed by a hyphen
$("input[id|='DiscountType']").each(function (i, el) {
//It'll be an array of elements
});

dnxit
- 7,118
- 2
- 30
- 34
-
Hello, how can I use a selector to select those elements whose id belongs to an array. – bpa.mdl May 17 '18 at 17:10
24
This selects all DIVs with an ID containing 'foo' and that are visible
$("div:visible[id*='foo']");

port-zero
- 657
- 3
- 7
-
If im searching for textbox elements rather than divs, is it simply $("input:visible[id*='foo']"); ? – user48408 Jul 30 '09 at 13:58
-
-
1
-
if you are trying to get the value of the elements ( in my case spans ) you have to get `$(this)[0].innerText` – Niklas Jul 21 '20 at 09:22
7
Thanks to both of you. This worked perfectly for me.
$("input[type='text'][id*=" + strID + "]:visible").each(function() {
this.value=strVal;
});

user48408
- 3,234
- 11
- 39
- 59