2

Is there any function for hiding 'DIV's or other html elements on right click, some tutorial, script, simple code javascript or jquery. Example: when is click on link or

tag or 'li' or 'Ul' or 'a' with right click to hide this element... How to do that?

UPDATE: Yes, all your solutions is OK but please view this code and why doesn' work:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script>
$(document).on("mousedown", "div, img, span, a", function () { 
    if (event.which === 3)
       $(this).hide();
});
</script>

<?php

$url = 'http://www.kupime.com/';

$data = file_get_contents($url);

$data = '<head><base href='.$url.' target="_blank" /></head>'.$data;




echo $data;


?>

And when I click nothing happend. WHY?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Andrew
  • 2,128
  • 3
  • 24
  • 42

3 Answers3

1
$("#yourDivId").click(function(e) {
 if(e.which === 3) { $(this).hide(); }
});
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

You can use event.which in your mousedown handler to surmise whether the user clicked with the left, or right mouse button.

$(document).on("mousedown", "div", function() { 
    if (event.which === 3)
       $(this).hide();
});

See this answer for more info on event.which

EDIT

Naturally, if you want to hide elements other than just div, you can comma-delimit them in on's selector

$(document).on("mousedown", "div, img, span, a", function () { 
    if (event.which === 3)
       $(this).hide();
});

Or if you want to hide anything that's right clicked

$(document).on("mousedown", "*", function () { 
    if (event.which === 3)
       $(this).hide();
});
Community
  • 1
  • 1
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • This is a great solution. But it seems that user1094631 wants to hide every element and not only divs (his comment: "but I need one function for all elements in html"), so it would be a good idea to add a note that 2nd parameter can be "skipped" :). – kubetz Dec 13 '11 at 15:30
-1

I found the answer right here. That will show you how to know if you clicked it with the right mouse button.

It will turn out to be like this:

$("body").children().click(function(e) {
 if(e.which === 3) { $(this).hide(); }
});
Community
  • 1
  • 1
Teun Pronk
  • 1,367
  • 12
  • 24
  • `click` is only triggered for left mouse button clicks. You must use `mousedown` to check for right-clicks :). – kubetz Dec 13 '11 at 15:56