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?
If you know the element type then: (eg: replace 'element' with 'div')
$("element[id$='txtTitle']")
If you don't know the element type:
$("[id$='txtTitle']")
// the old way, needs exact ID: document.getElementById("hi").value = "kk";
$(function() {
$("[id$='txtTitle']").val("zz");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="ctl_blabla_txtTitle" type="text" />
$('element[id$=txtTitle]')
It's not strictly necessary to quote the text fragment you are matching against
It's safer to add the underscore or $ to the term you're searching for so it's less likely to match other elements which end in the same ID:
$("element[id$=_txtTitle]")
(where element is the type of element you're trying to find - eg div, input etc.
(Note, you're suggesting your IDs tend to have $ signs in them, but I think .NET 2 now tends to use underscores in the ID instead, so my example uses an underscore).