Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Selector: Id Ends With?

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?

like image 267
Josh Stodola Avatar asked Mar 04 '09 05:03

Josh Stodola


People also ask

How to select id that ends with certain character using jQuery?

To get the elements with an ID that ends with a given string, use attribute selector with $ character.

Which of the following jQuery selector selects element with the given element ID some ID?

Selecting by ID If you want to select elements with a certain ID value, use the hash symbol (#) and the ID name.

How use contains in jQuery?

jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).


2 Answers

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" />
like image 156
Mark Hurd Avatar answered Sep 28 '22 08:09

Mark Hurd


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']") 
like image 22
Romain Guidoux Avatar answered Sep 28 '22 09:09

Romain Guidoux