Solved

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

Posted on 2016-09-20
3
41 Views
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 () {
    $(.AddFav).click(function(){........................

Thank you.
0
Comment
Question by:lapucca
3 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex 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 () {
    $('.AddFav').on('click',function(){
         var id = $(this).attr('id');  //assuming the element has an id assigned, this is how to retrieve it.

Open in new window

0
 

Author Closing Comment

by:lapucca
ID: 41807794
Thank you.
0
 
LVL 51

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 .../>
</div>

Open in new window


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

Open in new window


Saves having to add a class to each image
1

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now