View Single Post
  #2 (permalink)  
Old Apr 21st, 2008, 09:39
spinal007's Avatar
spinal007 spinal007 is offline
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: form element disable script

Step 1: Give your element IDs so that you can work with them via javascript
Step 2: Attach an "onchange" event to checkbox
Step 3: Use the value of "this.checked" to find out whether the checkbox is on/off
Step 4: Use that value to enable / disable the other elements

RESULT:
Code: Select all
<form action="" method="">
 <textarea name="Textarea1" id="Textarea1" cols="40" rows="5"></textarea>
 <input type="radio" name="radio1" id="radio1" value="show">
 <input type="Checkbox" name="Delete1" id="Delete1" onchange="
  document.getElementById('Textarea1').disabled = this.checked;
  document.getElementById('radio1').disabled = this.checked;
 " value="123">
</form>
ALTERNATIVE:
If you're starting up, I suggest you familiarize yourself with a library that takes care of browser compatibility for you. It can be very frustrating having to deal with these issues as an experienced developer - even more infuriating if you're a beginner.

Step 1: Give your element IDs so that you can work with them via javascript
Step 2: Get jQuery
Step 3: Use this code
Code: Select all
<form action="" method="">
 <textarea name="Textarea1" id="Textarea1" cols="40" rows="5"></textarea>
 <input type="radio" name="radio1" id="radio1" value="show">
 <input type="Checkbox" name="Delete1" id="Delete1" value="123">
</form>
<script language="javascript">
$(function(){
 $('#Delete1').change(function(){
  $('#Textarea1, #radio1').attr('disabled', this.checked);
 });
});
</script>
Techie note:
I'm sorry if I seem to be pushing everyone towards jQuery, but with a few years of experience, countless nights and cups of coffee under my belt, I think jQuery is the easiest way to introduce a beginner to jQuery - taking away the worries of cross browser compatibility.
Reply With Quote