•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,468 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,780 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 8534 | Replies: 7 | Solved
![]() |
| |
I have been trying to use
to get the value of this text box
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.
javascript Syntax (Toggle Plain Text)
<script language="javascript" type="text/javascript"> function check_length() { var x = document.getElementById("tbDescription").value; alert(x); } </script>
asp Syntax (Toggle Plain Text)
<asp:TextBox ID="tbDescription" runat="server" Height="78px" TextMode="MultiLine" MaxLength="10" Width="363px" ValidationGroup="quickQuote" onkeypress="check_length()" CausesValidation="true"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvDescription" runat="server" ControlToValidate="tbDescription" Display="none" ErrorMessage="You must give a description of your shipment." Font-Bold="True" SetFocusOnError="True" ValidationGroup="quickQuote"></asp:RequiredFieldValidator> <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.
I figured it out...
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:
to this everything worked fine. The actual text box in the code is still .
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:
<script language="javascript" type="text/javascript">
function check()
{
var maxLen = 50;
if (document.getElementById('tbDescription').value.length >= maxLen)
{
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
document.getElementById('tbDescription').value = document.getElementById('tbDescription').value.substring(0, maxLen);
}
}
</script>
<script language="javascript" type="text/javascript">
function check()
{
var maxLen = 50;
if (document.getElementById('ctl00_ContentPlaceHolderMain_tbDescription').value.length >= maxLen)
{
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
document.getElementById('ctl00_ContentPlaceHolderMain_tbDescription').value = document.getElementById('ctl00_ContentPlaceHolderMain_tbDescription').value.substring(0, maxLen);
}
}
</script>
<asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check()" Width="363px"></asp:TextBox>
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation:
Rep Power: 7
Solved Threads: 59
A better solution is this:
and for the TextBox have:
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.
javascript Syntax (Toggle Plain Text)
<script language="javascript" type="text/javascript"> function check_length(textbox) { var x = textbox.value; alert(x); } </script>
and for the TextBox have:
<asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check(this)" Width="363px"></asp:TextBox>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.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation:
Rep Power: 7
Solved Threads: 59
oops there's an error in my source
That's exactly what my solution does. You can use
<asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check_length(this);" Width="363px"></asp:TextBox>
•
•
•
•
What if I wanted to use information from another textbox?
That's exactly what my solution does. You can use
onkeypress="check_length(this);" for any textbox, no matter what its id is. Last edited by hollystyles : Sep 18th, 2007 at 4:23 am.
Your solution works for the textbox that the function call is initiated in only, right? If I wanted to use that value and another value from another text box or more in one function I could pass the value of one of the functions with "this", but how would I get the other textbox values?
Something like this:
Your solution worked great for what I originally needed, but I was wondering what I could do to use other textboxes too without having to check the source code to see what got added to the textbox2 id.
Something like this:
<asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check_length(this);" Width="363px"></asp:TextBox>
function check(textbox)
{
var x = textbox.value;
alert(x);
var y = document.getElementById("textbox2").value;
alert(y);
}Your solution worked great for what I originally needed, but I was wondering what I could do to use other textboxes too without having to check the source code to see what got added to the textbox2 id.
Last edited by rrocket : Sep 18th, 2007 at 9:11 am.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation:
Rep Power: 7
Solved Threads: 59
You can get the
Change the javascript function to have an extra parameter:
EDIT: missing bracket in serverside code snippet.
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:public void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.tbDescription.Attributes.Add("onkeydown", "check_length(this, document.getElementById('" + this.tbOtherTextBox.UniqueID + "'));");
}
}Change the javascript function to have an extra parameter:
function check(textbox1, textbox2)
{
var x = textbox1.value;
var y = textbox2.value;
alert(x + y);
}EDIT: missing bracket in serverside code snippet.
Last edited by hollystyles : Sep 18th, 2007 at 9:30 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- ASP.NET Registration Page (ASP.NET)
- Updated : Simple ASP.Net Login Page (ASP.NET)
- Fetching Form Values with ASP (ASP)
- How to transfer values between ASP.NET pages (ASP.NET)
- ASP.NET GridView order of operation (C#)
- jsp code for values from database to textbox (JSP)
- ASP.Net Security 101 Part 1 (ASP.NET)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- Retrieving values from pop up and displaying into asp page (ASP)
- Forms Authorization/ Authentication using asp .net and vb .net (ASP.NET)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Displaying RSS Feeds in Tabbed box on web page
- Next Thread: How to disable back button and backspace using Javascript..?



Hybrid Mode