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?
To get the elements with an ID that ends with a given string, use attribute selector with $ character.
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.
$() does not return the jQuery function, (which is $ itself,) but returns a wrapped set, with all those helpful methods on it.
ID and Element selector are the fastest selectors in jQuery.
$('[id ^=aName][id $=EditMode]')
Alternatively, you can use the "add()" function for readability, but I would recommend Chaos's method:
$("[id ^=aName]").add("[id $=EditMode]")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With