views:

597

answers:

2

Hi,

Is it possible for me waiting for a user to click a link, and upon clicking the link the link's text would be obtained?

Maybe by using onClick?

A: 

This is very simple using jQuery:

<script>
    $(document).ready(function(){
        $("a").click(function(){alert($(this).text());});
    });
</script>

Of course, you'll probably want to do something other than alert the text.

Steve Goodman
he want to know how to handle he click event in a firefox extension not in a html page
Marwan Aouida
whoops - my bad
Steve Goodman
+4  A: 

If you mean handling the click event for the links in the page that the user is browsing then this is how:

// handle the load event of the window  
window.addEventListener("load",Listen,false);  
function Listen()  
{   
gBrowser.addEventListener("DOMContentLoaded",DocumentLoaded,true);  
}  

// handle the document load event, this is fired whenever a document is loaded in the browser. Then attach an event listener for the click event  
function DocumentLoaded(event) {  
 var doc = event.originalTarget;
 doc.addEventListener("click",GetLinkText,true);  
}  
// handle the click event of the document and check if the clicked element is an anchor element.  
function GetLinkText(event) {  
   if (event.target instanceof HTMLAnchorElement) {  
    alert(event.target.innerHTML);
   }   
}
Marwan Aouida
wow this is great thanks
Lily