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 42

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 54

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add a 3rd selection to sum 7 29
PHP Form Calculate Total Price 10 43
jQuery slideToggle working incorrectly 3 24
How to create a textarea which saves text in HTML 8 37
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

803 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