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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With