Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Checkbox to Enable Input Box

Reply
Thread Tools

Checkbox to Enable Input Box

 
 
Ben
Guest
Posts: n/a
 
      06-03-2005
Here's what I have se far:

<script type="text/javascript">
other_text.disabled = true;
function other_text() {
other_text.disabled = (other_check.checked == false)? false :
true;
}
</script>
<input type="checkbox" id="other_check" name="other_check"
onchange="other_text()" /><label for="other_check">Other</label><br />
<input type="text" id="other_text" />


But it doesn't work.

I did have it working using a onchange on the checkbox and having the
"other_text" text box disabled by default, but this ment that if the
browser didn't support or have JS enabled then the text box would stay
disabled. So I need to disable it using javascript, and re-enable it
when the checkbox is checked.

Any suggestions?

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      06-03-2005
Ben wrote:
> Here's what I have se far:
>
> <script type="text/javascript">
> other_text.disabled = true;


2 things:

1) When that statement is executed, the other_text field doesn't exist
yet. That means it can't disable what doesn't exist.
2) The fieldName.property syntax is IE only and as such won't work in
any other browser.

document.formName.elementName.property;

Use the onload event handler to fire a function that does what you are
trying to do:

window.onload = someFunction;

function someFunction(){
document.forms['formName'].elements['other_text'].disabled;
}

Or better yet, just set your onload to point to your function

window.onload = other_text;

and ditch the first line.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
 
Reply With Quote
 
 
 
 
Ben
Guest
Posts: n/a
 
      06-03-2005
Still doesn't work. ;(

 
Reply With Quote
 
Random
Guest
Posts: n/a
 
      06-03-2005
Ben wrote:
> Here's what I have se far:
>
> <script type="text/javascript">
> other_text.disabled = true;


The element ID'd as 'other_text' doesn't exist at the time the above
line is executed. This should be done either onLoad or in a <script>
block after the <input id=other_text>

This, I think, is the crux of your problem.

> function other_text() {
> other_text.disabled = (other_check.checked == false)? false :
> true;


Change the name of the function or change the id of the text input.

You can also rewrite this more simply as:
other_text.disable = ( !other_check.checked );


> }
> </script>
> <input type="checkbox" id="other_check" name="other_check"
> onchange="other_text()" /><label for="other_check">Other</label><br />
> <input type="text" id="other_text" />
>
>
> But it doesn't work.
>
> I did have it working using a onchange on the checkbox and having the
> "other_text" text box disabled by default, but this ment that if the
> browser didn't support or have JS enabled then the text box would stay
> disabled. So I need to disable it using javascript, and re-enable it
> when the checkbox is checked.
>
> Any suggestions?



Consider instead:

<script type="text/javascript">

function endisable( ) {
document.forms['other_form'].elements['other_text'].disabled =
! document.forms['other_form'].elements[
'other_check'].checked;
}

</script>
<body onload="document.forms['other_form'].elements[
'other_text'].disabled=true;">
<form id="other_form">
<input type="checkbox" id="other_check" name="other_check"
onchange="endisable()" />
<label for="other_check">Other</label><br />
<input type="text" id="other_text" />

 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      06-03-2005
Thanks, that works fine, but still one problem.

Is there anyway to do it without the <body> tag?

 
Reply With Quote
 
Random
Guest
Posts: n/a
 
      06-03-2005
Ben wrote:
> Thanks, that works fine, but still one problem.
>
> Is there anyway to do it without the <body> tag?


Sure. Just execute this line of JS code:
document.forms['other_*form'].elements[
'other_text'].disabled=true;

Anywhere after the <input id=other_text /> tag.

 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      06-03-2005
Thanks again.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select box options to disable/enable checkbox? bigrich Javascript 2 01-18-2006 02:26 AM
How to enable people to pay me....(enable credit card payments in my web app) Patrick ASP .Net 1 11-10-2005 11:07 AM
Width of text input box vs. password input box cjl HTML 1 10-31-2005 11:46 AM
Off Topic: Width of text input box vs. password input box cjl Javascript 7 10-30-2005 10:11 PM
hide the input box or change the color of input box in html ashutosh Java 3 06-16-2005 02:21 PM



Advertisments