Bruce
Bruce

Reputation: 1071

Select based on multiple elements with jquery

I can't figure out the right way to ask this to get the proper answer and I am sure the answer exists all over the place, but ultimately I can't find it.

So how do I select based on multiple html elements?

$(function () {
$.post("draft-autosave.php", function (data) {
    $(".replyform,[name='sender_name']").not(".forwardform, [name='sender_name']").val(data.sender_name);
    $(".replyform,[name='receiver_name']").not(".forwardform, [name='receiver_name']")..val(data.receiver_name);
    $(".replyform,[name='subject']").not(".forwardform, [name='subject']").val(data.subject);
    $(".replyform,[name='body']").not(".forwardform, [name='body']").val(data.body);
}, "json");
setInterval(function () {
    $.post("draft-autosave.php", $("form").serialize());
}, 2000);
});

I and trying to say: select the 4 inputs based on name= from the form class="replyform" while not selecting the same input name= fields from a second form on the same page with class=forwardform.

Does this make it more clear?

Upvotes: 0

Views: 36

Answers (2)

MortenMoulder
MortenMoulder

Reputation: 6656

Seperate them using a comma.

$("[outterdiv1.classname], [innerdiv1.classname], [name='variable']").val(data.sender_name);

I am not sure if [name='variable'] is a valid selector, so you might want to add an element before the selector like div[name='variable'], but I am not sure.

Example: If your elements look like this:

<div class="number">
    <div class="innerdiv"></div>
</div>

You can select all those by doing:

$("div.number, .div.innerdiv")

How does your HTML look like? From looking at your example, some would cancel out each other, which you don't want. If you want to "not" select elements, you can do it like this:

$("selector").not("selectors")
//OR THIS
$("selector:not('selectors')")

Upvotes: 0

void
void

Reputation: 36703

$("outterdiv1.classname, innerdiv1.classname, [name='variable']").not("outterdiv2.classname, innerdiv2.classname, [name='variable']").val(whatever)

I hope this makes sense

Upvotes: 1

Related Questions