views:

44

answers:

5

How do I do a selection on a existing selection in jQuery? This is the given HTML

<div id='search'>
    <form action='' method='GET'>
        <input id="searchbutton" type='submit' name='qs' value='search'>
            <div id="searchoptions">
                <input type="checkbox" checked="checked" id="search_books book" name="search_books" value="1" /><label for="search_books">book</label>
                <input type="checkbox" checked="checked" id="search_movies movie" name="search_movies" value="1" /><label for="search_movies">movies</label>
            </div>
    </form>
</div>

The jQuery part:

$('#search').each(function(){
    //do stuff
    $(this).click(function(){
        alert('checkbox clicked');
    });
});

Of curse the click function triggers if the div gets clicked. But I'd like it to get triggered if the checkbox is clicked. I could just do $('#search input:checkbox') but I need to do some stuff with #search first. So how to I append a selector to $(this)?

e.g. $(this'input:checkbox')

Sorry if it's a fools question.

A: 

use your parent() and children() methods. You can specify selectors as arguments.

$(this).children(':checkbox');
Squirkle
Using `.children()` won't work in this situation because it only looks at immediate descendants. To look deeper, you need `.find()`.
patrick dw
+1  A: 

You would use .find() in this case, for example:

$(this).find(':checkbox');
//equiavlent to $('#search :checkbox')

There are many other tree traversal methods like this as well.

You can also do: $(':checkbox', this) but that's really just converted to the above by jQuery under the covers, so better to do it yourself IMO.

Nick Craver
+1  A: 

Use .find().

On another note, there's no reason to use .each() on $('#search') because that selector will only ever return 1 element, since HTML element IDs must be unique.

Matt Ball
+1 - Good point on using `.each()` against an ID selector.
patrick dw
I have to disagree that the `.each()` usage has anything to do with how many elements the set has. In fact it's not really related IMO, since `.bind()` (or pretty much any function) works on multiple elements, but even in a single element you may still want/need a `.each()`, for example: `$("#ID").each(function() { $(this).plugin({text: this.href}); });`
Nick Craver
I think the reason that the OP is using each() is because as he says "...I need to do some stuff with #search first", and that was their way of putting it into scope. But you are correct, each on an ID selector is usually not really needed.
Psytronic
thanks for that note.What should I write instead of .each() ?Sorry but I started coding with jQuery today and I try to follow some tutorials but I have some difficulties understanding the chained-function-concept.I actually understand how it's done but thinking this way is hard.
rotrotrot
If you want to bind a click listener to the div with ID `search`, just write `$('#search').click(function () { /* whatever */ } );`
Matt Ball
if you are looking to do lots of things with search and can't get the chaining right, you could put it into a self calling function: <code>(function(x){ //x will refer to $("#search");})($("#search"));</code> But whether this is a good idea someone else will tell you :)
Psytronic
Sorry I should've been more specific.There're multiple checkboxes that are preselected to checked.If a checkbox gets unchecked a ajax call should be executed sending every checked checkbox to a script.In my mind this is done by (pseudocode):checkbox gets clicked: -> get all checked checkboxes -> ajax calland it seems weird to $(':checkbox').click(function(){}) and in there go one level up to check all checkboxes which are checked or am I thinking in the wrong direction?
rotrotrot
It sounds like you may want to open a new question regarding this issue. At any rate, you can listen for `change` (not `click`) events on the parent since the event will bubble up from checkbox that fired it. Use [`.live()`](http://api.jquery.com/live/): `$('#search').live('change', function (event) { /* your ajax logic here */ });`
Matt Ball
A: 

Define a variable to contain your result set.

var $myVar = $('#search');

Then use the .add() method.

$myVar = $myVar.add($('#search input:checkbox'));
$myVar.click(function(){
    alert('div or checkbox clicked');
});
C Bauer
A: 

You can use

$(":checkbox", $(this));

where the second parameter is the context on which to search for elements which match the selector.

So

$(":checkbox", $(this)).click(function(){
    alert('checkbox clicked');
});
Psytronic