JavaScript problem: getElementsByName() doesn't work for me
document.getElementsByName(...) returns a collection of DOM elements.
Have you tried this?
document.getElementsByName("phone")[0].value
Based on the snippet of code, instead of using just names, you may want to use IDs instead. In that case, you can call...... HTML ...
<input type="text" id="phone" />
... Javascript ...
document.getElementById("phone").value
... to retrieve the element you wanted. getElementsByName not working
Here you go... seems:
onclick="javascript:
<--- not necessary - just reference the function nameShowContentEngineering
needs to be set in the window context- You're referencing the "value" attribute of an element that doesn't allow value attributes (h5)
Code
<input type="radio" name="Area" value="Engineering" id="EngineeringCheck" onclick="ShowContentEngineering();" />Engineering
<h5 name="EngineeringAreas"> WHAT THE HECK </h5>
<script>
window.ShowContentEngineering = function() {
alert(document.getElementsByName('EngineeringAreas')[0].innerHTML)
document.getElementById('InformationBlock').style.display = 'block';
}
</script>
Here's a working fiddle: https://jsfiddle.net/mu970a8k/ getElementsByName() not working?
document.getElementsByName()
returns a NodeList, so you have to access it by an index: document.getElementsByName('staff_counter')[0]
(depending on how many of these you have).
You also have access to a length
property to check how many Elements were matched.
Javascript getElementsByName.value not working
You have mentioned Wrong id
alert(document.getElementById("name").value);
if you want to use name
attribute then alert(document.getElementsByName("username")[0].value);
Updates:input type="text" id="name" name="username"
id is different from name JavaScript getElementByName doesn't work
It's getElementsByName
. Note the plural. It returns an array-like NodeList of elements with that name
attribute.
getElementsByName: doesn't work
The method getElementsByName
returns a node list which can be used like an array. What you need to do is grab them and loop over them to do the calculation for all rows.
function calc()
{
var amounts = document.getElementsByName("Amount");
var prices = document.getElementsByName("Price");
var sums = document.getElementsByName('Sum');
for(var i=0; i<amounts.length; i++)
{
sums[i].value = amounts[i].value * prices[i].value;
}
}
Using document.getElementsByName() isn't working?
getElementsByName
returns an HTMLCollection
. You can access the value of the first item like this:
document.getElementsByName("to").item(0).value
Or like this:document.getElementsByName("to")[0].value
More info:- https://developer.mozilla.org/en/DOM/HTMLCollection
Related Topics
Executing Code at Page-Level from Background.Js and Returning the Value
How to Replace Last Occurrence of Characters in a String Using JavaScript
What Is Ngdefaultcontrol in Angular
With JavaScript, How to Change the Z Index/Layer of an Svg <G> Element
Pass Parameter with Python Flask in External JavaScript
How to Build a Loop in JavaScript
Why Firefox Says That Window.Event Is Undefined? (Call Function with Added Event Listener)
Remove Zero-Width Space Characters from a JavaScript String
Omitting the Second Expression When Using the If-Else Shorthand
How to Sort an Associative Array by Its Values in JavaScript
React Jsx: Selecting "Selected" on Selected <Select> Option
JavaScript - Telling Setinterval to Only Fire X Amount of Times
What's "This" in JavaScript Onclick
How to Remove Text (Without Removing Inner Elements) from a Parent Element Using Jquery
How to Reset the Scale/Zoom of a Web App on an Orientation Change on the Iphone