Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6661013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:13:36+00:00 2026-05-26T02:13:36+00:00

document.getElementsByName(‘name’).value returns undefined . I don’t have the foggiest idea as to why this

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:13:36+00:00Added an answer on May 26, 2026 at 2:13 am

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

    var myvar = document.getElementsByName('myElement')[0].value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var brands = document.getElementsByName(brand); for(var brand in brands){ $(input[name='brand']).eq(brand).click(function(){ alert(hello22); loadDataFN(1); }); } This
document.onkeydown.toString() returns onkeydown function but I only want to get this function's name. Is
So, I have this between my head tags <script type=text/javascript> hidden_links = document.getElementsByName(javascript_needed); for
Consider this function: function validate() { var acc = document.getElementsByName('acc').value; var pass = document.getElementsByName('pass').value;
I cannot get this to work: function formvalidation() { var SiteNum= document.getElementsByName(sitesinput)[0].value; var i=1;
I have some code doing this : var changes = document.getElementsByName(from); for (var c=0;
Im trying to get some checkbox with a specific name document.getElementsByName(test); Unfortunatley i cant
I have HTML code like so: function toggle_action(type) { var tabs = document.getElementsByName(action_tab) for(var
<input type='text' id='txt' name='txtName' size='20' value='testing'/> <script type=text/javascript language='javascript'> var val = document.getElementsByName('txtName'); alert(val[0].value);
window.onload = function(e){ cells = document.getElementsByName('assetID'); for(j = 0; j < cells.length; j++) cells[j].style.display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.