savagenoob
savagenoob

Reputation: 413

Javascript to Disable/Enable form fields

I want this script to disable date fields when the page loads, then enable/disable depending on the drop down field. The script simply isnt doing anything, was wondering if anyone can see what I am doing wrong.

    <head>
    function formdisable(){
    var myTextField = document.getElementById('searchby');
if(myTextField.value = "agencyname")
    document.searchform.Date1.disabled=true
    document.searchform.Date2.disabled=true
    document.searchform.agencyname.disabled=false
else
    document.searchform.Date1.disabled=false
    document.searchform.Date2.disabled=false
    document.searchform.agencyname.disabled=true
    }
    onload = start;
    </script>
    </head>
    <table>
    <form id="searchform" name="searchform"> 
    <tr>
    <td>Search Term: </td><td><input tabindex="1" type="text" name="search" id="search1"        value=""  /></td>
    </tr>
    <tr><td>Search By:</td><td><select size="1"  id="searchby" name="searchby" onclick="formdisable()" tabindex="2">
<option value="agencyname">Agency Name</option>
<option value="daterange">Date Range</option>
    </select></td></tr>
    <tr>
    <td>Beginning Date: </td><td><input  id="Date1" name="Date1" size="10"   maxlength="10" value="<?php echo $_GET['Date1']?>"/> </td><td>Ending Date: </td><td><input   id="Date2" name="Date2" size="10" maxlength="10" value="<?php echo $_GET['Date2'];?>" />          </td></tr>
   <input type="hidden" id="wildcard" value="= 'Approved'" />  
   <input type="hidden" id="repid" value="<?php echo $_SESSION['REPID']; ?>" />  
   <tr><td><input tabindex="3" type='button' onclick='searchAgency()' name="searchsub"  value='Search' /></td></tr></td>
   </tr>
   </form>
   </table>

Upvotes: 0

Views: 3747

Answers (3)

Michael Berkowski
Michael Berkowski

Reputation: 270775

Right away, I see missing {} in your if/else, and an assignment = where an equality == belongs:

if(myTextField.value = "agencyname")
    document.searchform.Date1.disabled=true
    document.searchform.Date2.disabled=true
    document.searchform.agencyname.disabled=false
else
    document.searchform.Date1.disabled=false
    document.searchform.Date2.disabled=false
    document.searchform.agencyname.disabled=true

Should probably be:

<script type='text/javascript>
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Forgot the opening script tag

Then...

if(myTextField.value == "agencyname") {
    //-------------^^^^^^^
    // ==, not =
    document.searchform.Date1.disabled=true;
    // Please manually insert semicolons--^^^
    document.searchform.Date2.disabled=true;
    document.searchform.agencyname.disabled=false;
}
else {
    document.searchform.Date1.disabled=false;
    document.searchform.Date2.disabled=false;
    document.searchform.agencyname.disabled=true;
}

In your original version, you were assigning agencyname to myTextField.value inside the if() condition. Followed by that was a mess of partially executed code. The assignment would succeed, and then the first (and only the first) subsequent statement would execute as part of the if() block. The next two would execute before coming to a syntax error on the else.

You are relying on automatic semicolon insertion, which is a dangerous (mis-)feature of the JavaScript language when used recklessly. Please always terminate your statements with ;

Upvotes: 1

Imran Subhani
Imran Subhani

Reputation: 1104

I got to agree with Michael but you also missed opening tag

Upvotes: 0

PiTheNumber
PiTheNumber

Reputation: 23563

You are missing 6 ; two } and one { also you need to use == instead of =.

You should read a getting started tutorial.

Upvotes: 0

Related Questions