For example, there is a form as shown below:
<form id="my-form" method="post">
<input type="text" name="my-first-name" value="John">
<input type="text" name="my last name" value="Smith">
</form>
Then, you can use these selectors below according to 6.1. Attribute presence and value selectors and 6.2. Substring matching attribute selectors.
input[name]
selects all <input>
s which have name
attribute:
document.querySelectorAll('input[name]');
// "my-first-name" and "my last name"
input[name="my last name"]
selects all <input>
s whose name
attribute is exactly my last name
:
document.querySelectorAll('input[name="my last name"]');
// "my last name"
input[name~="name"]
selects all <input>
s whose name
attribute's whitespace-separated words contain my last name
:
document.querySelectorAll('input[name~="name"]');
// "my last name"
input[name|="my"]
selects all <input>
s whose name
attribute starts from my
with -
so my-
:
document.querySelectorAll('input[name|="my"]');
// "my-first-name"
input[name|="my last name"]
selects all <input>
s whose name
attribute is exactly my last name
:
document.querySelectorAll('input[name|="my last name"]');
// "my last name"
input[name^="my "]
selects all <input>
s whose name
attribute starts from my
:
document.querySelectorAll('input[name^="my "]');
// "my last name"
input[name$="ame"]
selects all <input>
s whose name
attribute ends with ame
:
document.querySelectorAll('input[name$="ame"]');
// "my-first-name" and "my last name"
input[name*="st-"]
selects all <input>
s whose name
attribute contains st-
:
document.querySelectorAll('input[name*="st-"]');
// "my-first-name"
In addition, input:not([name="my last name"])
selects all <input>
s whose name
attribute doesn't contain my last name
according to 4.3. The Negation (Matches-None) Pseudo-class: ':not()':
document.querySelectorAll('input:not([name="my last name"])');
// "my-first-name"
And, you can put form#my-form
in front of input[name^="my"]
to select <input>
s more specifically like logical AND (&&) as shown below:
document.querySelectorAll('form#my-form input[name^="my"]');
// "my-first-name" and "my last name"
And, you can also do input:not([name^="last"]):not([name*="st"][value="Smith"])
to select <input>
s more specifically like logical AND (&&) as shown below:
document.querySelectorAll('input:not([name^="my-"]):not([name*="st"][value="John"])');
// "my last name"
And, you can also do input[name*="first"], input[name="my last name"]
to select <input>
s more specifically like logical OR (||) as shown below:
document.querySelectorAll('input[name*="first"], input[name="my last name"]')
// "my-first-name" and "my last name"