Is there a selector
that I can query for elements with an ID that ends with a given string?
Say I have a element with an id of ctl00$ContentBody$txtTitle
. How can I get this by passing just txtTitle
?
Is there a selector
that I can query for elements with an ID that ends with a given string?
Say I have a element with an id of ctl00$ContentBody$txtTitle
. How can I get this by passing just txtTitle
?
An example: to select all
<a>
s with ID ending in _edit:or
The answer to the question is
$("[id$='txtTitle']")
, as Mark Hurd answered, but for those who, like me, want to find all the elements with an id which starts with a given string (for example txtTitle), try this (doc) :If you want to select elements which id contains a given string (doc) :
If you want to select elements which id is not a given string (doc) :
(it also matches the elements that don't have the specified attribute)
If you want to select elements which id contains a given word, delimited by spaces (doc) :
If you want to select elements which id is equal to a given string or starting with that string followed by a hyphen (doc) :
It's not strictly necessary to quote the text fragment you are matching against
Since this is ASP.NET, you can simply use the ASP <%= %> tag to print the generated ClientID of txtTitle:
This will result in...
... when the page is rendered.
Note: In Visual Studio, Intellisense will yell at you for putting ASP tags in JavaScript. You can ignore this as the result is valid JavaScript.
Try this:
In order to find an iframe id ending with "iFrame" within a page containing many iframes.