开发者

Javascript can't access document: null or undefined

开发者 https://www.devze.com 2023-04-12 09:07 出处:网络
document.getElementsByName(\'name\').value returns undefined.I don\'t have the foggiest idea as to why this is the case.

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>
  1. Does the type of button I have effect what information is available to the script?
  2. How 开发者_如何学编程do I find out why document.getElementsByName() returns undefined?
  3. Does the Javascript document object 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...


getElementsByName returns an array-like object containing elements, not an element. Stick a [0] before .value.

var myvar = document.getElementsByName('myElement')[0].value;


Javascript has 3 different functions which apply in situations like this.

 ___________________________________________________________________________________
|getElementById()       | Accesses the first element with the specified id.          |
|                       | Returns a single element, as ID's are unique in a document |
|_______________________|____________________________________________________________|
|getElementsByName()    | Accesses all elements with a specified name.               |
|                       | Returns an array of all elements marked with this name.    |
|                       | Used like a CSS class(not unique).                         |
|_______________________|____________________________________________________________|
|getElementsByTagName() | Accesses all elements with a specified tagname.            |
|                       | Returns an array of all elements with a given tag.         |
|_______________________|____________________________________________________________|

Use ID when you want a specific single element, name for an arbitrary group (access as an array with an index) and tag when you want all of the same type of element (also by indexed array). Good luck and happy coding ;-}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号