0

Is there a way to combine both an click event from an img element and a onkeypress event from an input element to trigger the same function?. This is what I got so far and it only services the click event...

  <div class="dataLine2">
        <img id ="daysText" src="images/days.png">
        <img class ="days" src="images/1day.png">
        <img class ="days" src="images/2days.png">
        <img class ="days" src="images/3days.png">
        <img class ="days" src="images/4days.png">
    </div>
    <input type="text" class="stateInput" placeholder="State" maxlength="2"  value="NY" onkeypress="myFunction(???)" />

    <script> 
        //  I suspect that jQuery can stack two triggers in some daisy chain, but it eludes me...
        $('.days').on('click', function (e){
            // Do somehting crazy with the "e" being critical to the process in this function
            // 
        }
    </script> 

Any help would be appreciated...

Dennis

DKean
  • 1,647
  • 5
  • 17
  • 26
  • You mean, using the same handler on click and keypress? This is not stacking. – Artjom B. Aug 15 '14 at 17:16
  • @Artjom Yes! Are you saying Impossible? – DKean Aug 15 '14 at 17:17
  • 1
    Duplicate of http://stackoverflow.com/questions/2534089/jquery-multiple-events-to-trigger-the-same-function – fpsColton Aug 15 '14 at 17:20
  • It's perfectly possible by defining the function which could handle both types of events and pass this function to `$days.on("click", func);` and `$input.on("keypress", func);` – Artjom B. Aug 15 '14 at 17:20
  • The trouble is that I need the (event) to identify who did what! – DKean Aug 15 '14 at 17:22
  • You can find out the event type with the incoming event variable ... ```$('.days').on('keypress click', function(event) { alert(event.type); // keypress or click });``` – Duane Aug 15 '14 at 17:26

2 Answers2

1

Just write your handler function and assign it:

    function foo(e) {
        // do something
    }

    $('.days').on('click', foo);
    $('input').on('keypress', foo);
Slippery Pete
  • 2,949
  • 1
  • 10
  • 13
  • The outcome, however is grim... It looks good and seems to make sense, but in reality, when the click is fired, occasionally the keypress follows suit and I get some bad results. It took me lots of time to figure this out. It is some form of cross pollination between the events. Thanks any way... – DKean Aug 15 '14 at 18:30
0

I was ducked 2 points for posting this question which purportedly is not original. Unfortunately, the other question/solution does not concern the same idea.

The unanimous solution for the other problem was:

`$('#element').on('keyup keypress blur change', function(event) {`

That is useless to me. In my setting I am not firing from the same element. I have 5 different elements each of which sends a trigger to the same function. And that is clearly stated in my post AND in the title!!!

So I should receive my 2 points back and then allow other points to pile on because I asked an original question, which has not yet been answered.

DK

DKean
  • 1,647
  • 5
  • 17
  • 26