...

View Full Version : javascript to extract info from textbox



animelover72
12-14-2009, 03:32 AM
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 ^^

jmrker
12-14-2009, 03:36 AM
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, :o ,but you should be able to do this:


var txtBox1Info = document.getElementById('txtbox1').value;

animelover72
12-15-2009, 11:19 PM
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:



//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.

joh6nn
12-16-2009, 12:16 AM
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.

animelover72
12-16-2009, 01:24 AM
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

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?

Old Pedant
12-16-2009, 02:26 AM
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:


<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:


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.

animelover72
12-16-2009, 03:41 AM
That worked brilliantly.

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

Thanks guys. Really appreciate it :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum