SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with a select object and javascript

    I'm trying to disable various fields on a html by using a onchange or onblur event in my selectfield. Rhe problem is, if I try to use it. it will only use one of the select options.

    Here is the function I am using:
    function purchAddress(Form) {
    if (Form.toLoan.value="Purchase")
    {
    Form.purAddress.disabled="True";
    Form.purCity.disabled="True";
    Form.purState.disabled="True";
    Form.purzCode.disabled="True";
    }
    else {
    Form.purAddress.disabled = "False";
    Form.purCity.disabled = "False";
    Form.purState.disabled = "False";
    Form.purzCode.disabled = "False";
    }
    }

    I realize some of that code is not needed, it's just the result of my tampering trying to get it to work.

    And here is the function call.

    <SELECT language=javascript id=Select1 onclick="return Select1_onclick()" onblur="purchAddress(shortForm)" name=toLoan>
    <OPTION value="Do not Know" selected>Do Not Know</OPTION>
    <OPTION value=Purchase>Purchase</OPTION>
    <OPTION value=Refinance>Refinance</OPTION>
    <OPTION value=""></OPTION></SELECT>

    Does anyone know how I can get this to work so that I can freely change values and have the function above still work. I've tried onchange and onblur, but both resutled in the same problem.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Magoo,

    using the value of a select as your test isn't the best way to go. What you really want to know is which option is selected (or in your case, whether the 2nd option is selected.

    Try this:

    function purchAddress(Form) {
    if (Form.toLoan.options[1].selected==true)
    // you could also say if (Form.toLoan.selectedIndex == 1)

    {
    Form.purAddress.disabled="True";
    Form.purCity.disabled="True";
    Form.purState.disabled="True";
    Form.purzCode.disabled="True";
    }
    else {
    Form.purAddress.disabled = "False";
    Form.purCity.disabled = "False";
    Form.purState.disabled = "False";
    Form.purzCode.disabled = "False";
    }
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok that allows the slect option to be changed. However if you select the option that disables the fields in question. You cannot have those fields reenabled by changing it to a different value.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you also need to remove the quotes from around true and false and , from the code you've included, I don't understand the purpose of the onclick method and why you're using onblur to call the function.

    Is this what you're trying to do?

    <script>
    function purchAddress() {

    var Form = document.forms[0];

    if (Form.toLoan.selectedIndex==1)
    {
    Form.purAddress.disabled=true;
    Form.purCity.disabled=true;
    Form.purState.disabled=true;
    Form.purzCode.disabled=true;
    }
    else {
    Form.purAddress.disabled = false;
    Form.purCity.disabled = false;
    Form.purState.disabled = false;
    Form.purzCode.disabled = false;
    }
    }
    </script>

    <form>
    <SELECT language=javascript id=Select1 onchange="purchAddress()" name=toLoan>
    <OPTION value="Do not Know" selected>Do Not Know</OPTION>
    <OPTION value=Purchase>Purchase</OPTION>
    <OPTION value=Refinance>Refinance</OPTION>
    <OPTION value=""></OPTION></SELECT>
    <input type="text" name="purAddress" value="xxx">
    <input type="text" name="purCity" value="xxx">
    <input type="text" name="purState" value="xxx">
    <input type="text" name="purzCode" value="xxx">
    </form>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •