Skip to main content

jQuery Selectors

jQuery Selectors​

Filtering by name attribute in Input elements​

Example From Here

$( "input[name!='newsletter']" )

$("input").not("[name=newsletter]")

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attributeNotEqual demo</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>

<div>
<input type="radio" name="newsletter" value="Hot Fuzz">
<span>name is newsletter</span>
</div>
<div>
<input type="radio" value="Cold Fusion">
<span>no name</span>
</div>
<div>
<input type="radio" name="accept" value="Evil Plans">
<span>name is accept</span>
</div>

<script>
$( "input[name!='newsletter']" ).next().append( "<b>; not newsletter</b>" );
</script>

</body>
</html>

image-20211102230352413

Negated Attribute Selector​

Select input elements within div elements with keys that are not equal to 'a' in the form

$( "form div:not(#a) input" )

$( "form div:not([id='a']) input" )

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attributeNotEqual demo</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>

<form class="">
<div id="a" class="form-floating mb-3">
<input type="email" class="form-control rounded-4" id="floatingInput" placeholder="[email protected]">
<label for="floatingInput">Email address</label>
</div>
<div id="b" class="form-floating mb-3">
<input type="password" class="form-control rounded-4" id="floatingPassword" placeholder="Password">
<label for="floatingPassword">Password</label>
</div>

</form>

<script>
$( "form div:not(#a) input" ).css("background-color","pink");
</script>

</body>
</html>

image-20211102231338241

For more selector usage please see here

Agreement
The code part of this work is licensed under Apache License 2.0 . You may freely modify and redistribute the code, and use it for commercial purposes, provided that you comply with the license. However, you are required to:
  • Attribution: Retain the original author's signature and code source information in the original and derivative code.
  • Preserve License: Retain the Apache 2.0 license file in the original and derivative code.
The documentation part of this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . You may freely share, including copying and distributing this work in any medium or format, and freely adapt, remix, transform, and build upon the material. However, you are required to:
  • Attribution: Give appropriate credit, provide a link to the license, and indicate if changes were made.
  • NonCommercial: You may not use the material for commercial purposes. For commercial use, please contact the author.
  • ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.