Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-20
3
Medium Priority
?
84 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
[X]
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
3 Comments
 
LVL 44

Accepted Solution

by:
zephyr_hex (Megan) earned 2000 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 59

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
Suggested Courses

705 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