document.getElementsByName('name').value returns undefined. I don’t have the foggiest idea as to why this is the case.
I included the .js file correctly, Firebug doesn’t find any errors in my code, and the code fails within the first line or two because each time I call the above mentioned function, it returns undefined. This has me completely confused.
Here is how I included the file:
<script type="text/javascript" src="new.js"></script>
Here is all the code needed to reach the first undefined value returned by a function call:
function myfunction() {
var myvar = document.getElementsByName('myElement').value;
…and I get undefined variables at that point. The button I use to trigger the script uses the following code:
<button type="button" onclick="myfunction()">MyButton</button>
- Does the type of button I have effect what information is available to the script?
- How do I find out why
document.getElementsByName()returnsundefined? - Does the Javascript
documentobject have any scope issues with scripts that are imported from another file?
Sorry if this is a dumb question. I’m very new to Javascript.
EDIT: Thank you Mike Samuel for your answer. That fixed it like a charm and I wonder why I didn’t know this before? I should have at leased guesses as much seeing as how getElementsByName() suggests there’s more than one value returned…
getElementsByNamereturns an array-like object containing elements, not an element. Stick a[0]before.value.