1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Js

Discussion in 'JavaScript and AJAX' started by Carlos, Mar 10, 2009.

  1. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Hi! I want to transfer the value of text box (name_txt) into variable var1, but there is some error. Please! Help me out.

    <script language="javascript" type="text/javascript" >
    <!--
    function my()
    {
    var var1;
    var1=document.getElementsByName(name_txt);
    document.write(var1);
    }
    -->
    </script>
    <input id="Text1" type="text" name="name_txt" />
    <input id="Button1" type="button" value="button" onclick="my()"/>

    </div>
    Thanx
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Try this

    var1=document.getElementsByName("name_txt");
     
  3. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Same error occurs again.... when i type in txtbox and click button
    error:A runtime error has occurred"
    Please! chk my code
    HTML:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="tax.aspx.cs" Inherits="tax" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>    
      <!--function PracticeWindow() {
    //window.open("tax.aspx","status,scrolling=yes,resizable,height=540,width=460");
    -->
    <script language="javascript" type="text/javascript" >
    
    function my() 
    {
    var var1;
    var1=document.getElementsByName("name_txt");
    document.write(var1);
    }
    </script>      
            <input id="Text1" type="text" name="name_txt" />
            <input id="Button1" type="button" value="button" onclick="my()"/>
        
        </div>
        </form>
    </body>
    </html>
     
    Last edited by a moderator: Mar 11, 2009
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    This works fine for me

    HTML:
    <script language="javascript" type="text/javascript" >
    <!--
    function my() 
    {
    	var var1;
    	var1=document.getElementsByName("name_txt");
    	document.write(var1.text);
    }
    -->
    </script>      
    <form id="form1" runat="server">
    	<input id="Text1" type="text" name="name_txt" value="1234567890"/>
    	<input id="Button1" type="button" value="button" onclick="my()"/>
    </form>
    
     
  5. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Please check this code again....

    "Undefined" is coming instead of "name_txt" value, when in browser.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    which browser ?
     
  7. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    I am using Internet Explore 6 and Mozilla Firfox.
    It's not working in anyone of them. and return "undefined"
     
  8. Prasanna Gopal

    Prasanna Gopal New Member

    Joined:
    Mar 11, 2009
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    0
    Code:
    <script language="javascript" type="text/javascript" >
    <!--
    function my() 
    {
    	var var1;
    	var1=document.getElementsByName("name_txt");
    	document.write(var1.[B]value[/B]);
    }
    -->
    </script>      
    <form id="form1" runat="server">
    	<input id="Text1" type="text" name="name_txt" value="1234567890"/>
    	<input id="Button1" type="button" value="button" onclick="my()"/>
    </form>
    That ought'a work..
     
  9. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Sorry! same results "Undefined".
    I have check this code on many PCs, but same results appear.

    document.write(var1);//////this line is working fine

    Through this code, i want to take input using Javascript and an HTML textbox.
    I think there is something else, which is missing in this code.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Try using getElementsById then and see ?
     
  11. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Hurray! Now it works, see the difference...
    Code:
    <!--
    function my() 
    {
    	var var1;
    	[B]var1=document.getElementById("Text1").value[/B];
    	document.write(var1);
    }
    --> 
    Thanx both of u.
     

Share This Page