JQuery id starts with selector | Double Marvellous
Back to Blog
Development Jul 16, 2024 4 min read

JQuery id starts with selector

JQuery id starts with selector

This is a handy way to target any element with a common string in the id. An element with id=”yourstring-whatever” would be targeted this way.

It does seem a little intensive on the DOM so maybe whittle down the elements to target it inside a container, thereby letting the browser know that it doesn’t have to go through the whole page, just to a container wherein it will find all the ids it might be looking for

Something like $(‘.yourcontainer [id^=”yourstring-“]’)

Anyway, enjoy!

$('[id^="yourstring-"]').on('touchstart click', function(e){ e.stopPropagation(); e.preventDefault(); console.log(this); }); };

Double Marvellous

AI Chatbot

Double Marvellous.