Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector for matching at start AND end of ID?

Tags:

jquery

I have a group of buttons on my web page which all have an id that end with the text "EditMode".

I'm using jQuery to assign an event to like so

$('[id $=EditMode]').click(editHandler); 

However now I want to prefix the id with a name (which I will know in advance) so the id would be "aName+someotherstuff+EditMode". How do I alter the above expression so I match on aName AS WELL as editMode?

like image 235
AJM Avatar asked Jul 17 '09 11:07

AJM


People also ask

What selector would I use to query for all elements with an ID that ends with a particular string?

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

Which is the correct form of using ID selector in jQuery?

The #id Selector The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

What does $() return in jQuery?

$() does not return the jQuery function, (which is $ itself,) but returns a wrapped set, with all those helpful methods on it.

Which is the fastest selector in jQuery?

ID and Element selector are the fastest selectors in jQuery.


2 Answers

$('[id ^=aName][id $=EditMode]') 
like image 50
chaos Avatar answered Oct 01 '22 14:10

chaos


Alternatively, you can use the "add()" function for readability, but I would recommend Chaos's method:

$("[id ^=aName]").add("[id $=EditMode]") 
like image 37
Richard Clayton Avatar answered Oct 01 '22 12:10

Richard Clayton