0

hi have this method in JS:

This one is for the button im using till now.

 $('#balpha').click(function(e) {
    e.preventDefault();
    if (alphasw < 1) { alphasw = 1;} 
    else { alphasw = 0;}
    $('.alpha').each(function(){
      /* element specific data object*/
      var data= $(this).data('maphilight') ;
        if (alphasw < 1) {
            data.alwaysOn = true;}
        else{
            data.alwaysOn = false;}
        $(this).trigger('alwaysOn.maphilight')
       });
      });

For some reason i have to make the same call with a input type="image" so I add onClick="alphabutton();" on input propertis and did next changes in the method.

function alphabutton(e) {
    e.preventDefault();
    if (alphasw < 1) { alphasw = 1;} 
    else { alphasw = 0;}
    $('.alpha').each(function(){
      /* element specific data object*/
      var data= $(this).data('maphilight') ;
        if (alphasw < 1) {
            data.alwaysOn = true;}
        else{
            data.alwaysOn = false;}
        $(this).trigger('alwaysOn.maphilight')
       });
    }

I cant see what is wrong, any advice?

I found a solution for my problem, probably I tried to make the things harder.

<input type="image" src="img/Buttons/alpha.png" id="balpha"/>

I just added id to the input tag, so I dont have to modify the first method.

Patrick Vibild
  • 351
  • 4
  • 16

3 Answers3

1

An option could be to use the jQuery multiple selector.

$('#balpha, input[type=image]').click(function () {
    // Stuff here
});

See demo

dreyescat
  • 13,558
  • 5
  • 50
  • 38
0

with

<input  type="image" onClick="alphabutton();"  />

you are getting error below line

    function alphabutton(e) {
       e.preventDefault();//error on this line
    }

according to this answer your code goes to:

 <input  type="image" onClick="alphabutton(event);"  />

DEMO

Community
  • 1
  • 1
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
0

Just connect the handler with jquery .click or .on as you did before and then you will have access to event parameter.

Another option is to return false at the end of your element onclick handler. (just native javascript)

easy
  • 320
  • 1
  • 10