0

I want to attach a 'click' event handler to the first child of an element with ID 'foo' using jQuery. I understand that the syntax for doing this is:

$('#foo:first-child').bind('click', function(event) { // I want to access the first child here }) 

Within the handler body I want to access the element which caused the event to be fired. I've read somewhere that you can't simply refer to it via 'this', so how can I access it?

3 Answers 3

2
$(this).doStuff() 
Sign up to request clarification or add additional context in comments.

Comments

2

Forget what you read somewhere and go ahead and use the this keyword:

$("#foo:first-child").click(function(event) { $(this).css("background", "pink"); }); 

Comments

0

Just use "this":

$('#foo:first-child').bind('click', function(event) { alert(this === $('#foo:first-child')); // True this.style.color = "red"; // First child now has red text. }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.