Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 576
Nick Jones Avatar asked Nov 15 '22 04:11

Nick Jones


1 Answers

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.

like image 189
Arun P Johny Avatar answered Dec 09 '22 16:12

Arun P Johny