Click here to Skip to main content
14,454,989 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a function that is attached when the page loads. It works:

Element is:
<img src="scroll.gif" id="leftScroll">


JS function is:
leftScroll.onmousedown = function(event) {


However, I'd like to attach it to 2 other elements as well, midScroll and rightScroll. How do I do this? I've tried this and several other variants and it doesn't work:

$("#leftScroll", "#midScroll", "#rightScroll").onmousedown( function(event) {


or this

$("#leftScroll", "#midScroll", "#rightScroll").onmousedown = function(event) {


What I have tried:

$("#leftScroll", "#midScroll", "#rightScroll").onmousedown( function(event) {


or this

$("#leftScroll", "#midScroll", "#rightScroll").onmousedown = function(event) {
Posted
Updated 14-Feb-20 12:33pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Have you thought about attaching the event to a class instead of an id
<img src="scroll.gif" id="leftScroll" class="scroller" />
<img src="scroll.gif" id="midScroll" class="scroller" />
<img src="scroll.gif" id="rightScroll" class="scroller" />
and then you should be able to just use
$(".scroller").onmousedown( function(event) {


Reference:
How to add an event listener to multiple elements in JavaScript[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100