Triggering a click event issues via (event.button != 0)

  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

I have an object that has a click event I'm trying to trigger. However in the click event I have the following if statement: if(event.button != 0){return true;}

This if statement allows right clicks to go through and activate but it also prevents me from triggering the event. Any ideas on how to prevent this? If I remove the if statement from the first click function everything works as intended.

Here's my example code based off of the jquery trigger event example 1:
Code: [ Select ]
<!DOCTYPE html>
<html>
<head>
 <style>

button { margin:10px; }
div { color:blue; font-weight:bold; }
span { color:red; }
</style>
 <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<a href="#" class="quoteLink">Button #1</a>
<a href="#" class="quotelink2">Button #2</a>

<div><span>0</span> button #1 clicks.</div>

<div><span>0</span> button #2 clicks.</div>
<script>
$(".quoteLink").live("click", function(event){
    if(event.button != 0){return true;}
    update($("span:first"));
});



$(".quotelink2").live("mouseover", function(event){
 $(".quoteLink").trigger('click');
    update($("span:last"));
});

function update(j) {
var n = parseInt(j.text(), 10);
j.text(n + 1);
}
</script>
</body>
</html>
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  <style>
  5. button { margin:10px; }
  6. div { color:blue; font-weight:bold; }
  7. span { color:red; }
  8. </style>
  9.  <script src="http://code.jquery.com/jquery-latest.js"></script>
  10. </head>
  11. <body>
  12. <a href="#" class="quoteLink">Button #1</a>
  13. <a href="#" class="quotelink2">Button #2</a>
  14. <div><span>0</span> button #1 clicks.</div>
  15. <div><span>0</span> button #2 clicks.</div>
  16. <script>
  17. $(".quoteLink").live("click", function(event){
  18.     if(event.button != 0){return true;}
  19.     update($("span:first"));
  20. });
  21. $(".quotelink2").live("mouseover", function(event){
  22.  $(".quoteLink").trigger('click');
  23.     update($("span:last"));
  24. });
  25. function update(j) {
  26. var n = parseInt(j.text(), 10);
  27. j.text(n + 1);
  28. }
  29. </script>
  30. </body>
  31. </html>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

For those of you who are stuck with this issue (I found the answer elsewhere), in this situation setting the trigger up like this should work:

$(".quoteLink").trigger({type:"click",button:0});

Hope that helps!

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 96 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.