Jump to content


Photo

[help] disable select tag using javascript


  • Please log in to reply
1 reply to this topic

#1 joe

joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 09 November 2010 - 12:26 AM

Hello everyone,
I need help to disable select tag using JavaScript, this is my code :
<html>
 <head>
  <script type="text/javascript">
   function disable()
   {
     if (window.document.form1.users.value == 0)
	 {
	    window.document.form1.test2.readOnly = true;
		window.document.form1.test.disabled = true; // this doesn't work
	 }
	 else
		window.document.form1.test2.readOnly = false;
		window.document.form1.test.disabled = false;
   }
  </script>
 </head>
<body>
 <form name="form1">
  <!--<select name="users" onchange="window.document.form1.nama_cabang.readOnly=false;">-->
   <select name="users" onchange="disable()">
   <!--<select name="users" disabled="disabled">-->
    <option value="">Select a person:</option>
	<option value="1">Bob</option>
  </select>
  <br>
  <select name="test" disabled="disabled">
    <option value="">Please select :</option>
	<option value="1">Yes</option>
  </select>
  <br>
  <input type="text" name="test2" readonly>
 </form>
 </body>
</html>

In my algorithm :
  • when user select "users" then "test2" and "test" would be rewritable.
  • But when "users" change to default value should be "test2" and "test" wouldn't be rewritable / readonly.
But why the 2nd algorithm doesn't work ? need your help, please... :)

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 November 2010 - 12:48 AM

Lmao, sorry, i have to laugh for a second; only because i missed this for a good 10 minutes while trying to figure out if the disabled property had any limitations on it of any kind. As it turns out, it doesn't, you just forgot to put the 'else' into it's own block.
function disable() 
   { 
     if (window.document.form1.users.value == 0) 
         { 
            window.document.form1.test2.readOnly = true; 
                window.document.form1.test.disabled = true; // this doesn't work 
         } 
         else
         {
                window.document.form1.test2.readOnly = false; 
                window.document.form1.test.disabled = false;
         }
   } 
That should work for you.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users