jQuery this selector function and limitations

0 votes

I need help understanding $(this). Is it possible to narrow the focus of "this" within the parentheses or does "this" preclude the use of any other attributes?

For example: I don't understand why this code:

$(this).children("div")

can't be rewritten like this:

$(this + " div")

without having to resort to something like:

$('#'+$(this).attr("id")+ " div")

Also, can you point me to 'this' in the jQuery documentation? It is difficult to use 'this' as a search term for obvious reasons.

Jun 23, 2022 in Web Development by gaurav
• 23,260 points
637 views

1 answer to this question.

0 votes

this isn't a jQuery "thing", but a basic JavaScript one. It can't be re-written the way you have in examples because it's an object, in particular either a DOM element or a jQuery object (depending on what context you're in). So if you did this:

 $(this + " div")

What you'd really be doing is calling .toString() on this to concatenate the strings, resulting in:

 $("[object Object] div")

....which isn't a valid selector.

answered Jun 23, 2022 by rajatha
• 7,680 points

Related Questions In Web Development

0 votes
0 answers

jQuery $( function() {} ) and $(document).ready the same?

To have a working datepicker on a ...READ MORE

Aug 22, 2022 in Web Development by gaurav
• 23,260 points
531 views
0 votes
0 answers

jQuery $(this).closest show & hide

I've got a table with multiple rows. ...READ MORE

Aug 12, 2022 in Web Development by gaurav
• 23,260 points
1,528 views
0 votes
0 answers

TypeError: this.client.incrementMaxListeners is not a function

I'm making a command for my discord ...READ MORE

Dec 29, 2020 in Web Development by anonymous
• 120 points
796 views
0 votes
1 answer

How do I select a sibling element using jQuery?

Use jQuery . siblings() to select the matching ...READ MORE

answered Jun 6, 2022 in JQuery by Edureka
• 13,690 points
471 views
0 votes
1 answer

How do I select a sibling element using jQuery?

jQuery siblings() Method The siblings() method returns all ...READ MORE

answered Jun 7, 2022 in JQuery by Edureka
• 13,690 points
2,413 views
0 votes
0 answers

jquery attribute selector escape doublequote

I want to select a html element ...READ MORE

Aug 11, 2022 in Web Development by gaurav
• 23,260 points
574 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
733 views
0 votes
1 answer
0 votes
1 answer

$("#select option:second").val() jquery - keep select second value of select box option

Yes, you want the special jQuery eq() selector. ...READ MORE

answered Jun 22, 2022 in Web Development by rajatha
• 7,680 points
3,284 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP