Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-20
3
Medium Priority
?
87 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 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 60

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

926 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