Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test a textbox with onkeyup

Status
Not open for further replies.

iaswnidou

Programmer
Joined
Apr 19, 2005
Messages
140
Location
GR
Hello

I want to enable/disable a few asp.net controls on a form depending on whether the user has typed anything in a textbox. I thought i could check this using the onkeyup event like this:

Code:
function EnableRadioList()
{
	var radiolist = document.getElementById('rdbRent');
	var text = document.getElementById('txtRent').value;
	if (text.length > 0)
	{
		radiolist.disabled = false;
	}
	else
	{
		radiolist.disabled = true;
	}
}

In the asp.net page i have:
Code:
<script language="javascript" src="Javascripts\Global.js"></script>
.
.
.
<asp:textbox id="txtRent" onkeyup="EnableRadioList()" runat="server"></asp:textbox>

Obviously it doesnt work! What am i doing wrong?Should i add the onkeyup event as an attribute to the textbox in codebehind indtead?

 
Is it because i am trying to disable a radiobuttonlist that renders as a span?
 
Add in some alerts to the EnableRadioList() function to determine if the function is even being called... to determine what it thinks the text.length is... to check which branch of the if-else it is taking.

Let us know how you go.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
I replaced the radiobuttonlist with radiobuttons and placed an alert inside the if loop and after the "disabled" statements and i see it fine. But they still remain disabled.

Maybe because i am not using the right id? Should i pass in the javascript function the ClientID?
 
The alerts return the correct lengths..

Code:
function EnableRadioList()
{
	var radio1 = document.getElementById('rdbWeek');
	var radio2 = document.getElementById('rdbMonth');
	var text = document.getElementById('txtRent').value;
		
	if (text.length > 0)
	{
		radio1.disabled = false;
		radio2.disabled = false;
		alert(text.length);
	}
	else
	{
		radio1.disabled = true;
		radio2.disabled = true;
		alert(text.length);
	}
}
 
I used the following test harness example and had no problem setting the radio buttons to disabled (and back again). See if this works on your end:
Code:
<html>
<head><title>Example</title>
<script type="text/javascript">
function toggleRadio() {
	var radio1 = document.forms[0].radio1;
	var radio2 = document.forms[0].radio2;
	radio1.disabled = (radio1.disabled) ? false : true;
	radio2.disabled = (radio2.disabled) ? false : true;
}
</script>
</head>
<body>
<form>
  <fieldset>
    <input type="radio" id="radio1">Example Radio 1<br/>
    <input type="radio" id="radio2">Example Radio 2<br/>
  </fieldset>
</form>
<a href="javascript:toggleRadio();">Toggle Radio Buttons</a>
</body>
</html>
If it does... then it may be something else on your page? Just a guess.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
It does work if the radiobuttons are HTML controls. What is the problem with the server ones?
 
Do a "view-source" on the web browser (using your code) and check it. There will be something "different" about it (when compared to my solution) - most likely the way ASP has created the HTML for that form element (or the form itself).

I don't use ASP enough to take this further, though.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top