This post will discuss how to get elements by name using JavaScript and jQuery.

1. Using jQuery

You can use the Attribute Equals Selector ([name='value']) to select elements with the given name in jQuery. Note, this might return more than one element since one can apply the same name to multiple elements within the document.

jQuery


HTML



Edit in JSFiddle

2. Using JavaScript

In pure JavaScript, you can use the native getElementsByName() method, which returns the NodeList of all elements having the given name.

JS


HTML



Edit in JSFiddle

 
Alternatively, you can use the querySelectorAll() method to get all elements within the document having the given name.

JS


HTML



Edit in JSFiddle

That’s all about getting an element by name in JavaScript and jQuery.