lapinkoira
lapinkoira

Reputation: 8988

Jquery attribute NOT contains selector

Is there a negative selector for this? https://api.jquery.com/attribute-contains-selector/

For example, to query all the inputs which property name havent a substring man

I tried the obvious but didnt work

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

<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">

<script>
$( "input[name*!='man']" ).val( "has man in it!" );
</script>

</body>
</html>

Uncaught Error: Syntax error, unrecognized expression:

This one is very close https://api.jquery.com/attribute-not-equal-selector/ but it's not equal, I would like to have not contains.

And this one jQuery "not contains" selector is not working because I dont have a text value, the way the form is created the value is within value property.

Upvotes: 1

Views: 3139

Answers (1)

DenseCrab
DenseCrab

Reputation: 1313

Use :not()

Select all inputs who do NOT(have a name containing 'man')

$( "input:not([name*='man'])" ).val( "DOESN'T have man in it!" );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">

Upvotes: 2

Related Questions