Nick Jones
Nick Jones

Reputation: 523

JQuery basics - Selecting elements inside a cached element

Sorry but this is a really basic jQuery syntax question, but I can't find anyone discussing it anywhere (probably as I don't know the correct terms to search for).

I want to select/cache a using a variable and then later get the value of checked inputs within that . Now I know I could do this without caching as:

var questionID = (a string)
$(questionID + " input:radio:checked").val()

But I use the div#'questionID' object a number of times so want to cache it i.e.

questionID = '#' + ...Something that changes ...
$question = $(questionID);

Now that $question is a jQuery object I can't work out how to select things inside it (without using children())

For example the following don't work:

$question.(' input:radio:checked)
$($question 'input:radio:checked')

I imagine this is a really basic bit of syntax, but I can't find it anywhere and I've tried lots of combinations with no luck......

Any help would be highly appreciated and apologies if this is really dumb but I'm very new to jQuery,

Nick

Upvotes: 5

Views: 1621

Answers (4)

Bobby Jack
Bobby Jack

Reputation: 16018

Try $('input:radio:checked', $question)...

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388336

You can use

var checkedRadios = $('input:radio:checked', $question);

OR

var checkedRadios = $question.find('input:radio:checked');

This is called tree traversing, you can find more about tree traversing api in jQuery Documentation

The find method is used to search for elements under a given scope.

Upvotes: 1

Jaime
Jaime

Reputation: 6814

I think your looking for

var checkedRadiosInsideQuestion = $question.find('input:radio:checked');

Upvotes: 0

Amjad Masad
Amjad Masad

Reputation: 4035

Use .find(), I think thats what your looking for!

$question.find('input:radio:checked');

EDIT I suggest skimming through jQuery API, http://api.jquery.com/category/traversing/tree-traversal/

Upvotes: 0

Related Questions