jQuery Find Element ID With Particular Pattern

jQuery Find Element ID With Particular Pattern

This is a quick jQuery code or script to find ID with custom pattern. The jQuery match uses pattern as example below:

$('span').each(function(){
    if( $(this).attr('id').match(/pattern/) ) {
        // your code goes here
    }
})

It depends on what kind of pattern you’re looking for. If your pattern is something like “jQueryFindID_83″, “jQueryFindID_67″, etc. then you could also use this code for partial match:

$("span[id^=jQueryFindID]")

There are 3 built-in attribute selectors for simple patterns. The jQuery code below find or get the element id that match with foo:

$("span[id^=foo]")

That selector matches all spans that have an id attribute and it starts or begins with foo (e.g. fooblah)

$("span[id$=foo]")

That selector matches all spans that have an id attribute ends with foo (e.g. blahfoo).

$("span[id*=foo]")

That selector matches all spans that have an id attribute and it has foo somewhere within in it (e.g. blahfooblah).


Jayesh Patel
Author
Jayesh Patel

Jayesh Patel is a Professional Web Developer & Designer and the Founder of InCreativeWeb.

As a highly Creative Web/Graphic/UI Designer - Front End / PHP / WordPress / Shopify Developer, with 14+ years of experience, he also provide complete solution from SEO to Digital Marketing. The passion he has for his work, his dedication, and ability to make quick, decisive decisions set him apart from the rest.

His first priority is to create a website with Complete SEO + Speed Up + WordPress Security Code of standards.



Explore

Related Articles

15th February, 2025

Advice on How to Produce Quality Seasonal Content

28th January, 2025

Best Affordable WordPress Hosting: Top Providers for 2025

18th January, 2025

Best 5 Elementor WordPress Themes (2025)

13th January, 2025

Three Easy Steps to Web Publishing on Github Pages