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 7655407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:30:31+00:00 2026-05-31T12:30:31+00:00

JavaScript $.each([‘#clk’,’#clk1′], function() { $(this).click(function () { alert("click") }); }); HTML <a href="#" id="clk">click

  • 0

JavaScript

 $.each(['#clk','#clk1'], function()
    {
        $(this).click(function () {
            alert("click")
        });
    });

HTML

   <a href="#" id="clk">click me</a>
   <a href="#" id="clk1">click me</a>

No alert box when the link is clicked.
UPDATE:
I have more than 1 id. I have shown only one to simplify the problem.

  • 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-31T12:30:32+00:00Added an answer on May 31, 2026 at 12:30 pm

    You will have to use String.toString() to get the value of the String object.

    It´s not clear why you would need an array of selectors but here are two solutions;

    Your solution using String.toString();

    // Array of strings to be used as element selectors.
    var selectors = ['#element1', '#element2'];
    
    // Using $.each()
    $.each(selectors, function() {
        // String.toString() returns the value of the String object.
        var $this = $(this.toString());
    
        $this.click(function () {
            console.log('Clicked element(1) =', this.id || this); // DEBUG
        });
    });
    

    Alternative solution using String.join();

    // Using String.join()
    $(selectors.join(',')).click(function(event) {
        event.preventDefault(); // This is to not follow the link
    
        // Notice that "this" now referes to the current/clicked element 
        // and not any string value from the "selectors" array.
        console.log('Clicked element(2) =', this.id || this); // DEBUG
    });
    

    ​

    See my demo.

    If you don´t really need the array of selectors I would recommend a simple multiple selector like this;

    $('#element1, #element2').click(function() { ... });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using javascript I'm looping through my H3 elements like this: $('h3').each(function(){ }); I'm then
Can't seem to get this to work, any ideas? http://jsfiddle.net/U92bM/ JavaScript: $('.articleSlide').each(function () {
I have a javascript that calls a function each 1 minute. That function sends
I'm trying to combine two separate javascript functions' outputs but each function is on
I am follwing this dom-based routing for kicking off specific javascript on each page
I need to run a javascript function each 10 seconds. I understand the syntax
In javascript if I need objects each to have an individual value of an
Various JavaScript libraries (e.g. jQuery) offer an each method: $.each(myObj, myFunc); What's the advantage
I use JavaScript for rendering 20 tables of 100 rows each. The data for
I wrote a simple javascript image rotator which picks a random image on each

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.