JavaScript Getelementbyname Doesn't Work

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:

  1. onclick="javascript: <--- not necessary - just reference the function name
  2. ShowContentEngineering needs to be set in the window context
  3. You're referencing the "value" attribute of an element that doesn't allow value attributes (h5)

I made it work instead grabbing the innerHTML of the 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



Leave a reply



Submit