Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select all inputs except under a specific id?

What I want to do is to select all the inputs buttons on the document, except those that reside under a specific id.

Example:

<body>
<input type="button">

<div id="something">
     <input type="button">
</div>
<div id="something2">
     <input type="button">
</div>


<input type="button">
<input type="button">
<input type="button">
</body>

For example, I would like to select all the inputs, except those that resides under the <div> whose id is "something".

What I've tried:

1) $('input[type="button"]:not(:parent(#something))').addCSS();

2) $('input[type="button"] :not(#something input[type="button"])')

And other similar approaches

like image 477
Jose A Avatar asked Aug 12 '12 23:08

Jose A


3 Answers

You can do it like this (relatively efficiently).

$("input[type=button]").filter(function() {
    return $(this).closest("#something").length == 0;
});

First, you get all the input[type=button] elements, then remove those with #something as a parent.

Another possibility is this:

$("input[type=button]").not("#something input[type=button]")

You'd have to test both to see which is more efficient, but either will work.

Working demo of both: http://jsfiddle.net/jfriend00/fjxDb/

like image 126
jfriend00 Avatar answered Nov 03 '22 08:11

jfriend00


You were almost there, all you needed to do was remove the space before your :not.

$('input[type="button"]:not(#something input[type="button"])')

I know this is an old question, but it shows up first on Google, and using jquery's .not() as shown in the accepted answer is not always an option.

like image 14
Niels Festjens Avatar answered Nov 03 '22 07:11

Niels Festjens


Maybe this works: $(":not(#something) input[type=button]")

like image 2
Lord Spectre Avatar answered Nov 03 '22 07:11

Lord Spectre