3

Suppose that I have a text with this html markup:

<p id="myId">Some text <span>some other text</span></p>

How can I get the value of the id when I right-click anywhere on the paragraph?

Update:

I'm just passing the value to a function, which I do not mention so as to not complicate it.

Iryn
  • 255
  • 2
  • 5
  • 13

3 Answers3

4

write a mousedown handler for p then

$('p').on('mousedown', function(e){
    if(e.which== 3){
        alert(this.id)
    }
})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

Here is the function:

 $('#myId').on('mousedown',function(event) {
if(event.which == 3){
    var i = $(this).attr('id');
    alert(i);
}

});

event.which() reports 1,2,or 3 depending on which button was clicked. Read here mode details http://api.jquery.com/event.which/

Maxim Ershov
  • 1,284
  • 1
  • 11
  • 15
0

Pure JavaScript version:

var myp = document.getElementsByTagName("p");
for(var i =0;i < myp.length;i++){
  myp[i].addEventListener("mousedown",function(e){
     if(e.which == 3){
     console.log(this.id);
     }
  },false);
}
StaleMartyr
  • 778
  • 5
  • 18