Last updated: Mar 5, 2024
Reading timeยท3 min

Use the querySelector() method to get an element by a name attribute.
The method returns the first element in the DOM that matches the provided
selector. If no element matches the selector, null is returned.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div name="box1">Box 1</div> <div name="box2">Box 2</div> <div name="box3">Box 3</div> <script src="index.js"></script> </body> </html>
And here is the related Javascript code.
// โ Get first element with name = `box1` const el1 = document.querySelector('[name="box1"]'); console.log(el1); // ๐๏ธ div // ---------------------------------------- // โ Get all elements with name = `box1` const elements1 = document.querySelectorAll('[name="box1"]'); console.log(elements1); // ๐๏ธ [div] // ---------------------------------------- // โ Get all elements with name = `box1` const elements2 = document.getElementsByName('box1'); console.log(elements2); // ๐๏ธ [div]

name attribute with a specific valueThe first example uses the
document.querySelector() method
to get the first element that has a name attribute with the value of box1.
// โ Get first element with name = `box1` const el1 = document.querySelector('[name="box1"]'); console.log(el1); // ๐๏ธ div

If no element in the DOM matches the selector, the method returns null.
name attribute with a specific valueIn the second example, we used the
document.querySelectorAll() method with
the exact same selector to get all of the elements that have a name attribute
with the value of box1.
const elements1 = document.querySelectorAll('[name="box1"]'); console.log(elements1); // ๐๏ธ [div]

The querySelectorAll method returns a NodeList containing the matches.
An alternative approach is to use the document.getElementsByName() method, which also returns a collection of the elements with the provided attribute.
const elements2 = document.getElementsByName('box1'); console.log(elements2); // ๐๏ธ [div]
You can also narrow things down by looking for a specific type of element, e.g.
a div that has a name attribute set to a certain value.
const el1 = document.querySelector('div[name="box1"]'); console.log(el1); // ๐๏ธ div
The example would only match a div element that has a name attribute set to
box1.
To get an element by partially matching its name attribute, use the
querySelector method with a selector that matches a name attribute whose
value starts with, ends with or contains a specific string.
// โ get element where name starts with `bo` const el1 = document.querySelector('[name^="bo"]'); console.log(el1); // ๐๏ธ div // ------------------------------------------------ // โ get element where name ends with `ox1` const el2 = document.querySelector('[name$="ox1"]'); console.log(el2); // ๐๏ธ div // ------------------------------------------------ // โ get element where name contains `box` const el3 = document.querySelector('[name*="box"]'); console.log(el3); // ๐๏ธ div
querySelectorAll method to get a collection of elements that match the selector.name attribute starting withThe first example gets the first DOM element that has a name attribute, whose
value starts with bo.
// ๐๏ธ get first element const el1 = document.querySelector('[name^="bo"]'); // ๐๏ธ get all elements const elements1 = document.querySelectorAll('[name^="bo"]');
^ symbol, which has the same meaning when used in regular expressions.name attribute ending withThe second example selects the first DOM element that has a name attribute
that ends with ox1.
// ๐๏ธ get first element const el2 = document.querySelector('[name$="ox1"]'); // ๐๏ธ get all elements const elements2 = document.querySelectorAll('[name$="ox1"]');
name attribute containingThe third example selects the first DOM element that has a name attribute that
contains box.
// ๐๏ธ get first element const el3 = document.querySelector('[name*="box"]'); // ๐๏ธ get all elements const elements3 = document.querySelectorAll('[name*="box"]');
box could be located anywhere in the value of the name attribute for the condition to be met.Note that you could also prefix the selector with a specific type of element you want to match to narrow down the results.
const el1 = document.querySelector('div[name^="bo"]');
The example selects a div element that has a name attribute that starts with
bo.
You can learn more about the related topics by checking out the following tutorials: