473,769 Members | 4,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checkbox to Enable Input Box

Ben
Here's what I have se far:

<script type="text/javascript">
other_text.disa bled = true;
function other_text() {
other_text.disa bled = (other_check.ch ecked == false)? false :
true;
}
</script>
<input type="checkbox" id="other_check " name="other_che ck"
onchange="other _text()" /><label for="other_chec k">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?

Jul 23 '05 #1
6 13138
Ben wrote:
Here's what I have se far:

<script type="text/javascript">
other_text.disa bled = 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.prope rty syntax is IE only and as such won't work in
any other browser.

document.formNa me.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2
Ben
Still doesn't work. ;(

Jul 23 '05 #3
Ben wrote:
Here's what I have se far:

<script type="text/javascript">
other_text.disa bled = 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.disa bled = (other_check.ch ecked == 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.disa ble = ( !other_check.ch ecked );

}
</script>
<input type="checkbox" id="other_check " name="other_che ck"
onchange="other _text()" /><label for="other_chec k">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="documen t.forms['other_form'].elements[
'other_text'].disabled=true; ">
<form id="other_form" >
<input type="checkbox" id="other_check " name="other_che ck"
onchange="endis able()" />
<label for="other_chec k">Other</label><br />
<input type="text" id="other_text " />

Jul 23 '05 #4
Ben
Thanks, that works fine, but still one problem.

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

Jul 23 '05 #5
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.

Jul 23 '05 #6
Ben
Thanks again. :)

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6172
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get values from a html form that consists of about 10 checkbox and a textbox where user have to key in a value to perform a search. From python tutors, I learned that I have to use the following method:
1
6218
by: David Wake | last post by:
I have two radio buttons and two checkboxes in a form. I'm trying to write some code so that when a radio button is selected, its corresponding checkbox is disabled. My code looks like this: function radioClicked(index) { document.table_config_form.my_checkbox.disabled = true;
1
3504
by: John Mullen | last post by:
I want to take the following HTLM and use javascript to turn on radio buttons if checkbox is checked, can I do this with javascript (maybe onClick or an array) or do i need a server side script ? <li>ABACAVIR SULFATE</li> <INPUT NAME="ingredient0" TYPE=checkbox VALUE="ABACAVIR SULFATE"><br> <input name="ABACAVIR SULFATE AMOUNT" type="radio" value="EQ 300MG BASE">EQ 300MG BASE<br> <input name="ABACAVIR SULFATE AMOUNT" type="radio" value="EQ...
8
4285
by: John | last post by:
Hello. I have a search form for music albums which among other things I need to search all the song titles of the song. Normally in a search form I would have checkboxes the user can use to select whether or not to search a given field. In this case it is not practical to have 20 checkboxes (each album can have up to 20 song titles).
6
3432
by: ML.Steve | last post by:
Hi, There are lots of posts on this subject but after a couple of hours of going though them I still can't get a number of fields to be disabled when a checkbox is ticked. Basically I have a lot of Forecast and Actual dates that can be entered, and next to each one an 'NA' checkbox that I want to disable/grey-out both dates (and the JavaScript calendar if possible). It also only has to work on Internet Explorer 6, as it is for...
6
23839
by: tshad | last post by:
I am trying to disable and enable a checkbox from javascript. The problem is that if the checkbox starts out as: <input id="Override" type="checkbox" name="Override"/> I can change it back and forth with no problems. using disabled = true or disabled = false If I start out as:
2
26372
by: Kevin | last post by:
I've been looking all over and I can't seem to find what ought to be simple. I need to disable a drop down when a checkbox is checked, and enable it when same checkbox is unchecked. I've found any number of scripts to disable / enable a drop down but they are all made to work with 2 different buttons, radio group selections, etc. I can't seem to come up with one that can toggle off a single checkbox.
4
4801
by: Richard Walker | last post by:
Hi I'm trying to disable/enable a group of checkboxes using javascript, but can't work out how to refer to them. eg: <form name="selectform" action="data.php" method="post"> <input name="days" type="checkbox" value="mon" />Monday <input name="days" type="checkbox" value="tue" />Tue <input name="days" type="checkbox" value="wed" />Wed <input name="all" type="radio" value="all"
3
1862
by: Patrick | last post by:
Hi, Newbish on JS here. Found this script on the web and it does what I want in FF, but works oddly in IE. When I check the box in IE nothing happens, but when I click on the body after the box is checked it displays the dropdown. Any ideas on how to make this work correctly in IE (displaying the dropdown when box checked)? Thanks for your help. <html> <head>
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.