I'm just trying to add a click function to all anchor tags whose ids are named by a specific pattern say 'some_ID1', 'some_ID2'etc. I referred the jquery documentation but I'm not able to get it. Here's the sample fiddle
<a href="#" id="some_id1"> some link 1 </a> <br>
<a href="#" id="some_id2"> some link 2 </a> <br>
<a href="#" id="some_id3"> some link 3 </a>
console.log("link is " + $("#a[id|='some_']"));
$("#a[id|='some_']").on ('click', function (e){
e.preventDefault();
console.log ("a click " + $("#a[id|='some_']"));
});
In your case simply use ^
(starts with selector) and remove #
from the selector as shown below :-
$("a[id^='some_id']").on('click', function (e){
e.preventDefault();
console.log ("a click " + this.id);
});
You can use attribute starts with selector. you also need to remove id selector #
from Element Selector a
:
$('a[id^="some_id"]').on ('click', function (e){
e.preventDefault();
console.log ("a click " + this.id);
});
Selecting ids starting width "some_":
$('a[id^="some_"]')
Selecting ids ending width "anid"
$('a[id$="some_"]')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.