Top jquery-selectors Questions

List of Tags

I have a layout similar to this

<div id="..."><img src="..."></div>

I would like to use a jQuery selector to select the child img inside of the div on click

To get the div, I've got this selector:

$(this)

How do I get the img with a selector?

Answered By: Simon ( 1270)

The jQuery constructor accepts a 2nd parameter which can be used to override the context of the selection.

jQuery("img", this);

Which is the same as

jQuery(this).find("img");

If the imgs are direct descendants of the clicked element, you can also use:

jQuery(this).children("img");

I have two radio buttons and want to post the value of the selected one, how can I get the value with jQuery?

I can get all of them like this:

$("form :radio")

But how do I know which one is selected?

Answered By: Peter J ( 791)

To get the value of the selected radioName item of a form called 'myForm':

$('input[name=radioName]:checked', '#myForm').val()
183
Joel Cunningham

I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with a jQuery selector.

I have looked for this myself but have been unable to find information on the syntax and how to use it. Does anyone know where the documentation for the syntax is?

EDIT: The attribute filters allow you to select based on patterns of an attribute value.

Answered By: Xenph Yan ( 142)

James Padolsey created a wonderful filter that allows regex to be used for selection.

Say you have the following div:

<div class="asdf">

Padolsey's :regex filter can select it like so:

$("div:regex(class, .*sd.*)")

Also, check the official documentation on selectors.