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?

3
  • 1
    stackoverflow.com/questions/1646851/jquery-right-click-event Commented Dec 13, 2011 at 15:21
  • I'm not a php guy, but I don't see any divs, spans, imgs, or anchors being generated there. Commented Dec 13, 2011 at 15:52
  • php generate all this elements:) Commented Dec 13, 2011 at 15:52

3 Answers 3

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

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" :).
1
$("#yourDivId").click(function(e) {
 if(e.which === 3) { $(this).hide(); }
});

6 Comments

but I need one function for all elements in html
yea, or you could add like: $("li, div, a, ...")
@user1094631 That is a bad idea. If you have 100 elements in a page it will add 100 event handlers + the $("*") selector on its own is very slow. Solution by Adam Rackis is the way to go - you want to minimize the amount of event handlers.
Solution of Adam is for div only though
Is it just me or nobody is noticing that this binds a "click" event which only fires for left mouse button...
|
-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(); }
});

1 Comment

click is only triggered for left mouse button clicks. You must use mousedown to check for right-clicks :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.