Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7035507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:18:44+00:00 2026-05-28T01:18:44+00:00

I have several elements on the html page, I call them through javascript like

  • 0

I have several elements on the html page, I call them through javascript like this:

$('#element_1_value')
$('#element_2_value')
$('#element_3_value')
$('#element_4_value')
$('#element_5_value')
.....

Now i have to create a single click event for all elements . How i can do this through regex.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:18:45+00:00Added an answer on May 28, 2026 at 1:18 am

    Here are four different ways to approach the problem:

    Option #1: You can put them all into one selector:

    $("#element_1_value, #element_2_value, #element_3_value, #element_4_value, #element_5_value").click(function() {
        // your code here
    })
    

    Option #2: Even better would be to assign a common class to all of them:

    <div id="element_1_value" class="valueCommon">
    <div id="element_2_value" class="valueCommon">
    ...
    

    And, then use this:

    $(".valueCommon").click(function() {
        // your code here
    })
    

    Option #3: It’s also possible to write selectors for what id’s that start and end with a given string, but I wouldn’t recommend this because it requires the selector engine to examine every single object in the page that has an id attribute which isn’t the fastest way of doing things:

    $("[id^='element_']").filter("[id$='_value']").click(function() {
        // your code here
    })
    

    Option #4: I’m not aware of any built-in jQuery support for regex matches in selectors. You can use a filter function where any arbitrary javascript could examine and object to decide if it matched of not, but the only way to use that with a regex would require creating a jQuery object of all objects in the page with an id and then running the filter on each one. That could work, but is certainly not the most effective way to do things. Using a common class allows the browser to optimize your queries much more than the regex filter.

    A regex match would look like this:

    $("[id]").filter(function() {
        return(this.id.match(/element_\d+_value/) != null);
    });
    

    This creates a jQuery object of all objects in the page that contain an id attribute and then compares each one to the regex, removing any element that doesn’t match. As I’ve said earlier, I would not recommend this for performance reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several html input elements which all look something like this: <input id=1
I've got several elements on a HTML page which have the same class -
I have several <asp:TextBox TextMode=MultiLine> elements on a page. On load, I populate them
I have several HTML blocks on a page set up like: <p class=something> <a
i have several common elements (components), that will generate some html. it seems my
I have several input and option elements on my page, each (well almost) have
I have several data that looks like this: Vector1_elements = T,C,A Vector2_elements = C,G,A
I have several <li> elements with different id's on ASP.NET page: <li id=li1 class=class1>
I have a page which has several <canvas> elements. I am passing the canvas
I have several 'select' elements on the page. When I choose some of options,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.