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 842701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:00:27+00:00 2026-05-15T06:00:27+00:00

<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);

  • 0
<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);
  alert(window.txtName.value);
</script>

In above code we are using

alert(val[0].value);

alert(window.txtName.value);

these two ways for getting value from object. What is the difference between both ways and which way is best.

  • 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-15T06:00:27+00:00Added an answer on May 15, 2026 at 6:00 am
    alert(window.txtName.value);
    

    This is the Wrong Way, which only works on IE. IE copies all named and IDd elements into properties of window and hence also global variables, which causes all sorts of problems. Don’t rely on this.

    The better way of doing it like that is:

    alert(document.forms[0].elements.txtName.value);
    

    assuming that the input was in the first <form> on the page. You can also use a form name, if it has one:

    alert(document.forms.someform.elements.txtName.value);
    

    and it’s also possible to shorten that:

    alert(document.someform.txtName.value);
    

    although I wouldn’t recommend doing so as you stand a greater chance of clashes between names and properties.

    This:

    alert(document.getElementsByName('txtName')[0].value);
    

    is OK, but since you already have id='txt' on the input it’s going to be simpler and faster to use that instead of relying on the non-unique name attribute:

    alert(document.getElementById('txt').value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a input type like this: <input name=adult-count type=text data-perunit=2500 data-maxunits=5 class=up-down-spinner value=0>
I have a input type radio with it's value. <input type=radio name=myradio id=radio1 value=someValue
Radio box code: <input type = radio name = choice value = A />Apples
<input type = 'text' name = 'name_'<?php echo $rownum'?> id = <?php echo $category_id
Loading http://diveintohtml5.ep.io/examples/input-type-date.html <form> <input name=birthday type=date> <input type=submit value=Go> </form> into Fx4 and Safari5
Given an input type text, you can clear the input with .val('') . How
I have the follow inputs type checkboxes: <input type=checkboxes value=3 name=mycheck>option1 <input type=checkboxes value=4
Hi in my input type text I want to call URL if user enters
I have an input type file element: <input type=file name=val1 /> And jquery: $(input[name='val1']).off(click);
How to set/change input value on other div click? Input type is hidden. It

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.