views:

94

answers:

2

hello. i made this :

<a href="<?=$rowz[0]?>" onClick="countLinks('<?=$row[6]?>','<?=$indexl?>')">[Link <?=$j++?>]</a>

the problem is that it doesn't work with middle button on IE or firefox. in fact, the countLinks using middle button is called only with chrome.

i think i need a Jquery function like mouseup event, just i don't know how call that function, which it calls countLinks with those parameters parameters.

any help?

cheers

+2  A: 

You're right. You need a mousedown or mouseup event to determine which mouse button was actually clicked.

But first of all, you need to get rid of that inline-event handler onclick and follow the shining road of unobtrusive javascript.

Thatfor you need to give that anchor a id or class tag to identify it (Of course you may also choose to select that anchor with a css selector). Lets assume we have added a class with the name myClazzz :)

javascript:

$(function(){
    $('.myClazzz').bind('mouseup', function(e){
        switch(e.which){
           case 1: {
              alert('LEFT');
           }
           case 2: {
              alert('MIDDLE');
           }
           case 3: {
              alert('RIGHT');
           }
        }
    });
});

The which property within a mousedown / mouseup event handler will contain a number which indicates which mousebutton was clicked.

jAndy
+1 but why not `click`? You can also see if clicked button is left, middle or right... Mouseup mousedown might behave differently cross browser.
Sinan Y.
@Sinan Y.: that javascript `click` event combines `mousedown` + `mouseup` and fires only on left button clicks.
jAndy
uhm... so i need to put for each case (1-2-3) a copy of the same function? and how can i pass parameters (like $row[6] and $indexl) to that function?
markzzz
@jAndy, i know that click is the combination of both, anyway my bad i remembered that it was behaving weird, probably i mixed up with sth else, but i tested now and you're definitely right, sorry for text pollution:)
Sinan Y.
+2  A: 

Here is a quick solution for you, by using some html5 attributes... Actually it was also possible before html5 but it wasn't validating.

I'd create the links as below:

<a class="myClazzz" href="<?=$rowz[0]?>" data-row="<?=$row[6]?>" data-index="<?=$indexl?>">...</a>

_here we put your parameters to data attributes

and write the js like this:

$(function(){
    //use mouseup, then it doesn't matter which button 
    //is clicked it will just fire the function
    $('.myClazzz').bind('mouseup', function(e){
        //get your params from data attributes
        var row   = $(this).attr("data-row"),
            index = $(this).attr("data-index");

        //and fire the function upon click
        countLinks(row,index);
    });
});
//don't forget to include jquery, before these lines;)

Hope this works out. Sinan.

PS myClazzz -> credits goes to jAndy :)

Sinan Y.
uoo...never seen before that syntax on link (i mean data-var stat). i need to learn it :) tomorrow i'll check that function, now in italy (3.00 am) it's a bit late! tnx man
markzzz