User Name Password Register
DaniWeb IT Discussion Community
All
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 392,007 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 4,163 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: 6488 | Replies: 7 | Solved
Reply
Join Date: Aug 2007
Posts: 54
Reputation: rrocket is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rrocket's Avatar
rrocket rrocket is offline Offline
Junior Poster in Training

Help getElementById not retreiving values from ASP textbox

  #1  
Sep 13th, 2007
I have been trying to use
  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
  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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Posts: 54
Reputation: rrocket is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rrocket's Avatar
rrocket rrocket is offline Offline
Junior Poster in Training

Solution Re: getElementById not retreiving values from ASP textbox

  #2  
Sep 14th, 2007
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:

<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>
to this
<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>
everything worked fine. The actual text box in the code is still
<asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check()" Width="363px"></asp:TextBox>
.
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 58
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: getElementById not retreiving values from ASP textbox

  #3  
Sep 14th, 2007
A better solution is this:
  1. <script language="javascript" type="text/javascript">
  2. function check_length(textbox)
  3. {
  4. var x = textbox.value;
  5. alert(x);
  6. }
  7. </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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Aug 2007
Posts: 54
Reputation: rrocket is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rrocket's Avatar
rrocket rrocket is offline Offline
Junior Poster in Training

Re: getElementById not retreiving values from ASP textbox

  #4  
Sep 17th, 2007
What if I wanted to use information from another textbox? Is there a way to call that text box with just the id or do I always have to check the source code and repaste that in my code?
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 58
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: getElementById not retreiving values from ASP textbox

  #5  
Sep 18th, 2007
oops there's an error in my source
<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 3:23 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Aug 2007
Posts: 54
Reputation: rrocket is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rrocket's Avatar
rrocket rrocket is offline Offline
Junior Poster in Training

Re: getElementById not retreiving values from ASP textbox

  #6  
Sep 18th, 2007
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:
<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 8:11 am.
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 58
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: getElementById not retreiving values from ASP textbox

  #7  
Sep 18th, 2007
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:

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 8:30 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Aug 2007
Posts: 54
Reputation: rrocket is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rrocket's Avatar
rrocket rrocket is offline Offline
Junior Poster in Training

Re: getElementById not retreiving values from ASP textbox

  #8  
Sep 18th, 2007
Great thanks.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 10:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC