Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery this and Selector $(this:first-child).css('color', 'red');

Is it possible to use a :selector with the following in jQuery?

$('.galler_attr').bind('click', function() {
   $(this:first-child).css('color', 'red'); 
      });
like image 761
hoerf Avatar asked Dec 02 '22 04:12

hoerf


1 Answers

No. You're trying to mix the function context with a string.

Use this as the context of the selector or call find() on $(this) to search within the DOM scope of the element. Either

$('.galler_attr').bind('click', function() {
   $(this).find(':first-child').css('color', 'red'); 
});

or this (which resolves to the above internally):

$('.galler_attr').bind('click', function() {
   $(':first-child', this).css('color', 'red'); 
});
like image 81
Russ Cam Avatar answered Jan 08 '23 06:01

Russ Cam