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

11th April, 2024

W3 Total Cache: A Comprehensive Review & Guide for 2024

8th April, 2024

Top 10 Must-Have Web Design Resources and Tools for 2024

8th April, 2024

Unleash Accessibility Potential in Front-end Development