Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript to extract info from textbox

    Hi,

    I currently have a asp.NET page which has a textbox with the ID: "txtbox1"
    I would like javascript to store the data entered into txtbox1 into a var.

    I have tried using
    var jVarName;
    jVarName = '<%#aVarName%>';

    but so far it hasnt worked.

    Any suggestions would be appreciated ^^

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,028
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb Consider this ...

    Quote Originally Posted by animelover72 View Post
    Hi,

    I currently have a asp.NET page which has a textbox with the ID: "txtbox1"
    I would like javascript to store the data entered into txtbox1 into a var.

    I have tried using
    var jVarName;
    jVarName = '<%#aVarName%>';

    but so far it hasnt worked.

    Any suggestions would be appreciated ^^
    I don't know a thing about asp.NET, ,but you should be able to do this:
    Code:
      var txtBox1Info = document.getElementById('txtbox1').value;

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for that. I'm trying the getElementById now but I keep getting a null value instead of the value in the text box. maybe i should have been a little less vague in my post >< Sorry about that.

    Here is my code so far:

    Code:
    //For the js code:
    
    function page_Button2_OnClick()
    {
        var result = true;
    
    var TEXTHERE = document.getElementById("recordsAccountNo").value;
    alert(TEXTHERE);
    
        return result;
    }
    
    
    //For the asp.NET data counterpart
    
    <mt:Record PreserveParameters="Get" DataSourceID="recordsDataSource" ID="records" runat="server"><ItemTemplate>
           <table class="Record" cellspacing="0" cellpadding="0">
              <mt:MTPanel id="Error" visible="False" runat="server">
              <tr class="Error">
                <td colspan="2"><mt:MTLabel id="ErrorLabel" runat="server"/></td>
              </tr>
              </mt:MTPanel>
              <tr class="Controls">
                <td class="th"><label for="<%# records.GetControl("AccountNo").ClientID %>">Account No&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td> 
                <td><mt:MTTextBox Source="AccountNo" DataType="Integer" Required="True" Caption="Account No" maxlength="10" Columns="10" ID="AccountNo" runat="server"/></td>
              </tr>
      </table>
    </ItemTemplate></mt:Record>
    <mt:MTDataSource ID="recordsDataSource" ConnectionString="InMotion:CreditApp" SelectCommandType="Table" InsertCommandType="Table" UpdateCommandType="Table" DeleteCommandType="Table" runat="server">
       <SelectCommand>
    SELECT * 
    FROM records {SQL_Where} {SQL_OrderBy}
       </SelectCommand>
       <InsertCommand>
    INSERT INTO records([AccountNo])
     </InsertCommand>
       <UpdateCommand>
    UPDATE records SET [AccountNo]={AccountNo})
      </UpdateCommand>
       <DeleteCommand>
    
    //...and so forth for the rest of the sql data extraction...
    
    
    
    //For the HTML counterpart:
    
    <form id="records" name="{HTMLFormName}" action="{Action}" method="post">
      <table cellspacing="0" cellpadding="0" border="0">
        <tr>
          <td valign="top">
            <table class="Header" cellspacing="0" cellpadding="0" border="0">
              <tr>
                <td class="HeaderLeft"><img alt="" src="Styles/Compact/Images/Spacer.gif" border="0"></td> 
                <td class="th"><strong>Add/Edit Records </strong></td> 
                <td class="HeaderRight"><img alt="" src="Styles/Compact/Images/Spacer.gif" border="0"></td>
              </tr>
            </table>
     
            <table class="Record" cellspacing="0" cellpadding="0">
              <!-- BEGIN Error -->
              <tr class="Error">
                <td colspan="2">{Error}</td>
              </tr>
              <!-- END Error -->
              <tr class="Controls">
                <td class="th"><label for="recordsAccountNo">Account No&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td> 
                <td><input id="recordsAccountNo" maxlength="10" size="10" value="{AccountNo}" name="{AccountNo_Name}"></td>
              </tr>
    
    //...and so forth for the rest of the data that is extracted
    This page basically extracts SQL data and dumps it into the text box on the page. I dont think that Javascript can directly extract data from SQL so i did it in this round a bout way of dumping it into a text box.

    Any suggestions or comments would be helpful.
    Last edited by animelover72; 12-15-2009 at 10:32 PM.

  • #4
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    does your javascript occur in the page before the form does? if so, the browser won't have rendered the form yet, so it won't exist/have a value. you'll need to either move the javascript after the form, or put it into an onLoad event.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #5
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    i just did some further testing for this.
    The code for the button is now after the page and grid loads event. However it stil returns with a null value.
    I have also tried entering
    Code:
    var TEXTHERE = document.getElementById("somethingrandom").value;
    alert(TEXTHERE);
    When i dont have an id called somethingrandom and it returned with a null value as well. Could there be something wrong in the getElementById code that isnt really communicating with items on the page?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    The ID that you specify in your ASP.NET code is often *mangled* by ASP.NET when it produces the corresponding HTML element.

    There is an ASP.NET property that allows you to find out what the ID is in the browser: clientID.

    Your own code is using that here:
    Code:
    <label for="<%# records.GetControl("AccountNo").ClientID %>">Account No
    So you need to do something similar in getting the ID to use in the JS code.

    You could try this:
    Code:
    var TEXTHERE = document.getElementById("<%# records.GetControl("AccountNo").ClientID%>").value;
    alert(TEXTHERE);
    In any case, you can find out what the ID really is by bringing up the page in your browser and then clicking on the VIEW menu and then on the SOURCE menu item. Find that <input> field and see what the ID *really* is, not the ASP.NET version thereof.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That worked brilliantly.

    I never knew that the HTML code can change when ASP.NET produces it.

    Thanks guys. Really appreciate it


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •