Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  142] [ 6]  / answers: 1 / hits: 32460  / 14 Years ago, mon, october 18, 2010, 12:00:00

Hypothetical: I find a page with a button ('#bigButton'), that when clicked causes a llama ('img#theLlama') to show() using jQuery.



So, somewhere (say, Line 76) in buttons.js:



$('#bigButton').click(function(){
$('img#theLlama').show();
})


Now, let's say I have a big HTML page with a whole bunch of .js files included. I can click on the button and see the llama appear, but I have no idea where the code above is.



The solution I am looking for is very similar to that which is available with CSS in Firebug. I want to inspect the element and have it show me that this jQuery occurs in Line 76 of buttons.js, along with any other bindings on this element.



*Note: The bounty is for a specific answer to the 'llama question,' ie pointing to a the solution that is described above. *



FireQuery is a great tool for many jQuery tasks, but it does not seem to answer the llama question. If I am wrong about this, please correct me.


More From » jquery

 Answers
6

Using Firebug, FireQuery and this fiddle:



Hitting Cmd+Shift+C (Inspect Element) and clicking on the button reveals this:
Screenshot



Clicking on the events Object {click= } reveals this (after expanding some info)



Screenshot



And clicking on the function() reveals this:

Screenshot



Which should be the code you are looking for, right?



As a note, Firebug can't always find the exact line of code something came from. I've had this method fail completely! Another approach is to use named function expressions. Changing the code to:



$('#bigButton').click(function showMyLlama(){
$('img#theLlama').show();
})


Now reveals this when inspecting the events object:



alt



Which is way more useful than just function() as it is now obvious that this handler shows us a llama. You can also now search the code for the function name and find it!






Using Chrome, its built in web inspector and this fiddle:



Hitting Cmd+Shift+C (Inspect Element) and clicking on the button shows this:

Screenshot



Clicking on the button in the elements inspector then pressing Escape to open the JS Console:
Screenshot



In the Chrome Console, $0 refers to the selected element in the elements panel.



Typing in $._data( $0 ) will give us the jQuery (internal) data object which includes events, just like in our Firebug example, sadly, Chrome wont let us click on the function, but it will let us see the source:



<Broken Screenshot link>






A note about .live() events:



Live Events are stored on $._data( document, events ) and contain an origHandler that points at the function:



<Broken screenshot link>


[#95276] Friday, October 15, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;