JavaScript get element by name
The reason you're seeing that error is because document.getElementsByName
returns a NodeList
of elements. And a NodeList
of elements does not have a .value
property.
Use this instead:
document.getElementsByName("acc")[0].value
Vanilla JS - Get element by name of a div
Use getElementById
to get the tag with id 'edit_pickup_date_modal'. Than search with querySelector
for the first INPUT-field with name = 'type' and set the value.
document.getElementById('edit_pickup_date_modal').querySelector('input[name=type]').value=2;
<div id='edit_pickup_date_modal'>
<div>
<input name ='type'>
</div>
</div>
Get elements by name that contain part of a string using Javascript
You can do it like this using querySelectorAll
let ele = document.querySelectorAll('select[name^=visitors]')
console.log(ele)
<select name="visitors10"> <option class="disableDuration4" value="5">5</option> <option class="disableDuration5" value="6">6</option> <option class="disableDuration6" value="7">7</option></select><select name="visitors11"> <option class="disableDuration4" value="5">5</option> <option class="disableDuration5" value="6">6</option> <option class="disableDuration6" value="7">7</option></select><select name="visitors12"> <option class="disableDuration4" value="5">5</option> <option class="disableDuration5" value="6">6</option> <option class="disableDuration6" value="7">7</option></select><select name="xyz"></select><select name="visitorsxyz"></select>
Get name of form element
var name = element.getAttribute("name");
Get element by part of Name or ID
Your best bet is probably document.querySelectorAll
, which you can use any CSS selector with, including an "attribute starts with" selector like input[id^="id_qtedje_"]
. It's supported on all modern browsers, and also IE8:
var elements = document.querySelectorAll('input[id^="id_qtedje_"]');
If you wanted just the first match (rather than a list), you could use document.querySelector
instead. It returns a reference to the first match in document order, or null
if nothing matched.
Alternately, you could give the elements a class name, then use document.getElementsByClassName
, but note that while getElementsByClassName
was supported in old versions of Chrome and Firefox, IE8 doesn't have it, so it's not as well-supported as the more-useful querySelectorAll
in the modern era.
var elements = document.getElementsByClassName("theClassName");
If you use any libraries (jQuery, MooTools, Closure, Prototype, etc.), they're likely to have a function you can use to look up elements by just about any CSS selector, filling the gaps in browser support with their own code. For instance, in jQuery, it's the $
(jQuery
) function; in MooTools and Prototype, it's $$
.
get child node by name in javascript
document.querySelector('input[name="test1"]')
Or if you want to target a specific child of a specific element:
var test = document.querySelector('#test')
var test1 = test.querySelector('input[name="test1"]');
window.console.log(test1)
Related Topics
Show Hide Divs on Click in HTML and CSS Without Jquery
Html "Overlay" Which Allows Clicks to Fall Through to Elements Behind It
Html 5 Video or Audio Playlist
Adding Additional Data to Select Options Using Jquery
How to Use CSS (And JavaScript) to Create a Blurred, "Frosted" Background
Print ≪Div Id="Printarea"≫≪/Div≫ Only
Instead of Using Prefixes I Want to Ask Site Visitors to Upgrade Their Browser
How to Make the Browser Wait to Display the Page Until It's Fully Loaded
Inject CSS Stylesheet as String Using JavaScript
How to Access Previous Promise Results in a .Then() Chain
How to Display JavaScript Variables in a HTML Page Without Document.Write
Saving Binary Data as File Using JavaScript from a Browser
How to Pass Parameter to Function Using in Addeventlistener
How to Prevent JavaScript Injection Attacks Within User-Generated Html
Programmatically Selecting Text in an Input Field on iOS Devices (Mobile Safari)
Setting CSS Pseudo-Class Rules from JavaScript