following is my code
HTML :
<div id = "content" >
<div class = "child" >Hello !! </div>
</div>
Javascript :
$(function() {
$('#content .child').click(function() {
//print the selected jQuery parameters
});
});
I need to capture parameters which I pass to jQuery
function in above code,
I want to print the output as #content .child
.
Thank you !!
Write your own wrappers for click, focus, .. events that provide this functionality. Writing such a wrapper is fairly trivial. Here's one way:
Your event handler function now gets two params instead of one. First is event, and the second is the selector that was used to bind this event.
The advantage of wrapping it up with closures is that you can bind multiple click events to the same element with different selectors, and they will all work together.
See an example.
Normally you'd use
selector
, but in this context,this
has no selector. You might use:Of course, that will show the same selector for each child, and not all that useful (maybe debugging). You can go simpler, mind you:
Working example: http://jsfiddle.net/cmuTv/
Apart from Kobi's answer, it's not generally possible. It's kind of like asking "given the following loop how do I get the original array back again?"
It's clear that in this case, you can't get the original array back given only
a
. But what might not be clear is that the same is true in your case as well.This is because a call to
.click()
is essentially turned into a call to.each()
and that's basically just afor
loop around each of the matched elements. When you're looking at a single element in that collection, there's no way to get the original collection back again.