2

I would like to use a saved selector and drill down further with it. In this case I would like to access the link tag contained in the element with the id

html

<div id='selectedElement'>
   <a href="#">some link</a>
</div>


var $selector = $('#selectedElement');
1

3 Answers 3

5

Just use .find()

<div id='selectedElement'>
   <a href="#">some link</a>
</div>

var $selector = $('#selectedElement'),
    $anchor = $selector.find('a');
Sign up to request clarification or add additional context in comments.

Comments

2
var $selector = $('#selectedElement');

// a will contain a reference to all <a> elements that are a descendent of the
// the element with id "selectedElement"
var a = $selector.find('a');

since $selector has been assigned a jQuery object which contains references to elements in the DOM that match the selector #selectedElement, you can use the $selector variable to drill down further.

Comments

2

you can easily use the same way:

var selector = $('#selectedElement');

alert( selector.find("a").attr("href") );

1 Comment

there isn't one that works in all browsers without additional addons/plugins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.