How to get an element by name attribute in JavaScript

How to get an element by name attribute in JavaScript

In this post we will give you information about How to get an element by name attribute in JavaScript. Hear we will give you detail about How to get an element by name attribute in JavaScriptAnd how to use it also give you demo for it if it is necessary.

we are going to learn how to get/select an HTML element by using a name attribute in JavaScript.

Consider we have a <input> elements like this.

&lt;input name="email" placeholder="email" /&gt;&lt;input name="password" placeholder="password" /&gt;

Now, we need to get the above two elements by using name attribute.

The document.querySelector method

To get the element by name attribute, we can use the document.querySelector() method by passing [attribute = value] as an argument.

Here is an example:

const email = document.querySelector('[name=email]');const password = document.querySelector('[name=password]');console.log(email);console.log(password);

Hope this code and post will helped you for implement How to get an element by name attribute in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US