jQuery Selector: Id Ends With?

JqueryJquery Selectors

Jquery Problem Overview


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?

Jquery Solutions


Solution 1 - Jquery

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']")

More information available


// 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" />

Solution 2 - Jquery

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) :

$("[id^='txtTitle']")

If you want to select elements which id contains a given string (doc) :

$("[id*='txtTitle']")

If you want to select elements which id is not a given string (doc) :

$("[id!='myValue']")

(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) :

$("[id~='myValue']")

If you want to select elements which id is equal to a given string or starting with that string followed by a hyphen (doc) :

$("[id|='myValue']")

Solution 3 - Jquery

Try

$("element[id$='txtTitle']");

edit: 4 seconds late :P

Solution 4 - Jquery

$('element[id$=txtTitle]')

It's not strictly necessary to quote the text fragment you are matching against

Solution 5 - Jquery

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).

Solution 6 - Jquery

An example: to select all <a>s with ID ending in _edit:

jQuery("a[id$=_edit]")

or

jQuery("a[id$='_edit']")

Solution 7 - Jquery

Since this is ASP.NET, you can simply use the ASP <%= %> tag to print the generated ClientID of txtTitle:

$('<%= txtTitle.ClientID %>')

This will result in...

$('ctl00$ContentBody$txtTitle')

... 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.

Solution 8 - Jquery

Try this:

<asp:HiddenField ID="0858674_h" Value="0" runat="server" />

var test = $(this).find('[id*="_h"').val();

Solution 9 - Jquery

In order to find an iframe id ending with "iFrame" within a page containing many iframes.

jQuery(document).ready(function (){		
				  jQuery("iframe").each(function(){				  		
 					if( jQuery(this).attr('id').match(/_iFrame/) ) {
							alert(jQuery(this).attr('id'));
						
					 }					 
				  });	  
		 });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosh StodolaView Question on Stackoverflow
Solution 1 - JqueryMark HurdView Answer on Stackoverflow
Solution 2 - JqueryRomain GuidouxView Answer on Stackoverflow
Solution 3 - JquerykkyyView Answer on Stackoverflow
Solution 4 - JqueryScott EverndenView Answer on Stackoverflow
Solution 5 - JqueryNick GilbertView Answer on Stackoverflow
Solution 6 - JqueryAnton KraievyiView Answer on Stackoverflow
Solution 7 - JqueryMichaelView Answer on Stackoverflow
Solution 8 - JquerypawelView Answer on Stackoverflow
Solution 9 - JqueryneelmegView Answer on Stackoverflow