Steve
Steve

Reputation: 4898

Working with the body element

I have a page (figure below) with a div#content centered in the body. I want to right-click in the body area, outside of div#content, and read the color there. If I try

 $(document).on("contextmenu", "body", function(el){
      bodyBackgroundColor = $(el).css('background-color');
      debugger;
 });

I don't catch the interrupt when I click in the wings.

If I try

 $(document).on("contextmenu",function(el){
          bodyBackgroundColor = $(el).css('background-color');
          debugger;
     });

I catch the interrupt but with el = document, the $(el).css('background-color') doesn't work.

How should I be doing this?

Thanks.

body element

Upvotes: 0

Views: 71

Answers (1)

PSL
PSL

Reputation: 123739

I think you need to get the background-color applied on the element where you right-clicked. If so try this.

Demo

el in the callback is not the element it is the event, you can get the element as e.target or even event.srcElement would work

$(document).on("contextmenu",function(e){

         bodyBackgroundColor = $(e.target).css('background-color');
          alert(bodyBackgroundColor);
     });

Upvotes: 2

Related Questions