473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

retrieve textbox value

hi friends,

i m using external javascript in my webapp. if i want to access the content of a text box within that external script, i should i do?
Sep 12 '07 #1
14 8672
gits
5,390 Expert Mod 4TB
hello,

welcome to TSDN ...

use:

Expand|Select|Wrap|Line Numbers
  1. var value = document.getElementById('textbox_id').value;
note: the textbox must have an id that you may use for 'textbox_id' ...

kind regards
Sep 12 '07 #2
gits
5,390 Expert Mod 4TB
i changed the thread-title to better describe the problem ... please: always use a good thread title

kind regards
Sep 12 '07 #3
rrocket
116 100+
I have been trying to use
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.                         function check_length()
  3.                         {
  4.                             var x = document.getElementById("tbDescription").value;
  5.                             alert(x);
  6.                         }
  7.  
  8. </script>
to get the value of this text box
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="tbDescription" runat="server" Height="78px" TextMode="MultiLine" MaxLength="10"
  2.                                         Width="363px" ValidationGroup="quickQuote" onkeypress="check_length()" CausesValidation="true"></asp:TextBox>
  3.                                         <asp:RequiredFieldValidator ID="rfvDescription" runat="server" ControlToValidate="tbDescription"
  4.                                         Display="none" ErrorMessage="You must give a description of your shipment." Font-Bold="True"
  5.                                         SetFocusOnError="True" ValidationGroup="quickQuote"></asp:RequiredFieldValidator>
  6.                                         <cc1:ValidatorCalloutExtender id="co_rfvDescription" runat="server" TargetControlID="rfvDescription"></cc1:ValidatorCalloutExtender>
but get an object required error in IE and document.getElementById("tbDescription") has no properties in FireFox. I have tried using the single, double quotes, and nothing around the tbDescription in the javascript function, but none of the 3 helps.
Sep 13 '07 #4
gits
5,390 Expert Mod 4TB
hi ...

set the id ... it is case-sensitive and you used ID

kind regards
Sep 13 '07 #5
rrocket
116 100+
I changed it to this
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check()" Width="363px"></asp:TextBox>
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.                         function check()
  3.                         {
  4.                             var x = document.getElementById('tbDescription').value;
  5.  
  6.                             alert(x);
  7.                         }
  8.                         </script>
  9.  
but still get the same errors.

this does not help either and returns null in the alert
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.                         function check()
  3.                         {
  4.                             var x = document.getElementById('tbDescription');
  5.  
  6.                             alert(x.value);
  7.                         }
  8.                         </script>
  9.  
Sep 13 '07 #6
gits
5,390 Expert Mod 4TB
hmmm ...

can you post the html-code? when the page is loaded in the browser have a look at view source and search for the textbox and its id ... may be it is not 'tbDescription'?

kind regards
Sep 13 '07 #7
rrocket
116 100+
Here is the portion with the textbox in it... The rest is pretty big, but I will post it if you want.

Expand|Select|Wrap|Line Numbers
  1. <div id="ctl00_ContentPlaceHolderMain_panelDescription">
  2.                         <table>
  3.                             <tr>
  4.                                 <td>
  5.                                     <strong><span style="color: maroon">*Description Of Shipment</span></strong></td>
  6.                             </tr>
  7.                             <tr>
  8.                                 <td>
  9.                                     <textarea name="ctl00$ContentPlaceHolderMain$tbDescription" rows="2" cols="20" id="ctl00_ContentPlaceHolderMain_tbDescription" onkeypress="check()" style="height:78px;width:363px;"></textarea>
  10.                                     <span id="ctl00_ContentPlaceHolderMain_rfvDescription" style="color:Red;font-weight:bold;display:none;"></span>
  11.  
  12.                                         </td>
  13.                             </tr>
  14.                         </table>
  15.                         <script language="javascript" type="text/javascript">
  16.                         function check()
  17.                         {
  18.                             var x = document.getElementById('tbDescription').value;
  19.  
  20.                             alert(x);
  21.                         }
  22.                         </script>
  23.                     </div>
  24.  
Sep 13 '07 #8
gits
5,390 Expert Mod 4TB
hi ...

as you can see ... the id is not 'tbDescription' ... you have to use the actual id of your textbox ...

kind regards
Sep 13 '07 #9
rrocket
116 100+
I figured it out... Thanks Gits for the suggestion!



When I looked at the source code on the actual webpage the text box id changed from id=tbdescription to

ctl00_ContentPlaceHolderMain_tbDescription. So once I changed the code from this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="javascript" type="text/javascript">
  3.  
  4. function check()
  5.  
  6. {
  7.  
  8. var maxLen = 50; 
  9.  
  10.  
  11. if (document.getElementById('tbDescription').value.length >= maxLen) 
  12.  
  13. {
  14.  
  15. var msg = "You have reached your maximum limit of characters allowed";
  16.  
  17. alert(msg);
  18.  
  19. document.getElementById('tbDescription').value = document.getElementById('tbDescription').value.substring(0, maxLen);
  20.  
  21. }
  22.  
  23.  
  24.  
  25. }
  26.  
  27. </script>
  28.  
  29.  
to this
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="javascript" type="text/javascript">
  3.  
  4. function check()
  5.  
  6. {
  7.  
  8. var maxLen = 50; 
  9.  
  10.  
  11. if (document.getElementById('ctl00_ContentPlaceHolderMain_tbDescription').value.length >= maxLen) 
  12.  
  13. {
  14.  
  15. var msg = "You have reached your maximum limit of characters allowed";
  16.  
  17. alert(msg);
  18.  
  19. document.getElementById('ctl00_ContentPlaceHolderMain_tbDescription').value = document.getElementById('ctl00_ContentPlaceHolderMain_tbDescription').value.substring(0, maxLen);
  20.  
  21. }
  22.  
  23.  
  24.  
  25. }
  26.  
  27. </script>
  28.  
  29.  
everything worked fine. The actual text box in the code is still
Expand|Select|Wrap|Line Numbers
  1.  
  2. <asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check()" Width="363px"></asp:TextBox>
  3.  
  4.  
.
Sep 14 '07 #10
gits
5,390 Expert Mod 4TB
hi ...

glad to hear you got it working ...

i'm not very familiar with asp/asp.net so it seems that the id is rendered to another value when the asp-code is executed ... have that in mind for further coding :) ... may be there is a way to set a 'fixed' id? ... because it is annoying to have the code executed and look at the html-source-code to gather the real id that an element is getting? may be someone in the asp-forum may help here ...

kind regards
Sep 14 '07 #11
rrocket
116 100+
This is actually a solution to that issue... Something so simple, but overlooked by me.

Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check(this)" Width="363px"></asp:TextBox>
-------------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.                         function check(textbox)
  3.                         {
  4.                            var maxLen = 500; 
  5.  
  6.                             if (textbox.value.length >= maxLen) 
  7.                                 {
  8.                                     var msg = "You have reached your maximum limit of characters allowed";
  9.                                     alert(msg);
  10.                                     textbox.value = textbox.value.substring(0, maxLen);
  11.                                  }
  12.                         }
  13.                         </script>

I got that from hollystyle on another forum...
"Now it doesn't matter what the textbox is called as it passes a reference to itself to the javascript function. This is loosley coupled programming the function's responsibilty is to check the length of a textbox value and that's it, it shouldn't have 'knowledge' of the textbox's id."
Sep 14 '07 #12
gits
5,390 Expert Mod 4TB
hi ...

:) yes of course ... but in case you ever would have to know the id ... may be in case you wnat to set a value of a textbox from another control the 'this'-trick wouldn't work :) ... so i thought about any possibility to set a fixed id to an asp-element so that you would know your real-id at designtime already?

kind regards
Sep 15 '07 #13
rrocket
116 100+
I got this from another response on another forum:

You can get the this.tbDescription.UniqueID server side. UniqueID is the hierarchical id the control will have when rendered to the client. You could add the onkeydown attribute serverside something like:

Expand|Select|Wrap|Line Numbers
  1. public void Page_Load(object sender, EventArgs e)
  2. {
  3.     if(!IsPostBack)
  4.     {
  5.         this.tbDescription.Attributes.Add("onkeydown", "check_length(this, document.getElementById('" + this.tbOtherTextBox.UniqueID + "'));");
  6.     }
  7. }
Change the javascript function to have an extra parameter:

Expand|Select|Wrap|Line Numbers
  1. function check(textbox1, textbox2)
  2. {
  3. var x = textbox1.value;
  4. var y = textbox2.value;
  5. alert(x + y);
  6.  
  7. }
Sep 18 '07 #14
gits
5,390 Expert Mod 4TB
hi ...

thanks a lot for sharing your findings ... may be it will help other users to solve their issues they may have ...

kind regards
Sep 18 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mark Brown | last post by:
I'm new to ASP.NET and I'm trying to create an aspx page that retrieves info from a database, let's the user make a change and write the change back. It reads from the database just fine. The...
1
by: Bryan | last post by:
Hello: I need to retrieve the value of a textbox control in a asp.net datagrid using Javascript. Can anybody help out. I believe the control clientID needs to be passed client side so the...
0
by: Xiru | last post by:
I would like to retrieve the value from a DataList on postback. I can read the ItemIndex and values from a child TextBox but not cannot seem to find a way to read a databound item on the datalist....
2
by: epigram | last post by:
I'm responding to a button click event on an asp.net web form. I then need to retrieve the value from a TextBox control and I want to compare it against the control's previous value to see if it...
2
by: Frank | last post by:
Can I do this? I add a session var in C# and ultimatly want to pass it into a vbscript client side activeX control. This is what I have so far but get " Object Required:'name2' " error. Can...
3
by: Kees de Winter | last post by:
Hi, If I have a TextBox place inside a content placeholder then at runtime the TextBox's name changes to ctl00_ContentPlaceHolder1_tbCity. What is the best way to get the value of the TextBox...
1
by: Cem | last post by:
Hi, in GridView1 I have following code: <ItemTemplate> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("airport_active", "images/icons/ico_airport_active_{0}.gif") %>'...
2
by: durga2005 | last post by:
Hi I m using webgrid control I m trying to add a new record using its footer In the footer I placed a button,and one textboxe to insert the new record. when I click the add button,the data in...
2
by: phpachu | last post by:
Hi, I hav created a group of textboxes using a loop and its names are unique and names are assigned using variable ( like <input type=text name=$name1>). Then How can i retrieve the values in...
2
by: chirag1989 | last post by:
i have created an arry of text box now i want to retrieve the values of each text box so as to insert them in data base can anyone help how can i retrieve the values of array of textbox and store...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.