Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

How to select multiple object and take action on Click event?

Posted on 2016-09-20
Last Modified: 2016-09-21
Hi, I have 4 images that when users click on it I would like to preform action  What's the best way to select them for the click event?  And is my syntax correct?   I would like to take action when any one of the image is clicked.

$(document).ready(function () {
    $("#Id1, #Id2, #ARUP, #Id3, #id4'").click(function(e) {......

or should I assign a class to all these image?
$(document).ready(function () {

Thank you.
Question by:lapucca
LVL 43

Accepted Solution

zephyr_hex (Megan) earned 500 total points
ID: 41807764
Assign a class.  It's more extensible and easier to maintain.  If you need to know the specific image that was clicked, you can use $(this):

$(document).ready(function () {
         var id = $(this).attr('id');  //assuming the element has an id assigned, this is how to retrieve it.

Open in new window


Author Closing Comment

ID: 41807794
Thank you.
LVL 55

Expert Comment

by:Julian Hansen
ID: 41808151
Another option is to put the images in a container with a class
<div class="active-images">
   <img .../>
   <img .../>
   <img .../>

Open in new window

And then
$('.active-images img').click(

Open in new window

Saves having to add a class to each image

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question