Results 1 to 3 of 3

Thread: Drop down boxes and javascript - How to get the value

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    79

    Drop down boxes and javascript - How to get the value

    Good Day to all!
    I am working on a function that is making my head hurt. I know it's something simple so hopefully one of you great people will point me in the right direction:
    I created this function:
    Code:
            
    function getCompanyFaxAndPhone(document.getElementById('ddlCompanyName').value) {
                Case "None":
                    //None Selected
                    document.PayoffEForm.txtPhoneNumber.value = '';
                    document.PayoffEForm.txtFaxNumber.value = '';
                    break;
                Case "Alaska Escrow & Title Insurance":
                    document.PayoffEForm.txtPhoneNumber.value = "somePhone";
                    document.PayoffEForm.txtFaxNumber.value = "someFax";
                    break;
            }
    that is to be used on an onchange event of the drop down list, like so:
    <select id="ddlCompanyName" name="ddlCompanyName" onchange="getCompanyFaxAndPhone()">
    <option value="None">None</option>
    <option value="something1">something1</option>
    <option value="something2">something2</option>
    <option value="something3">something3</option>
    </select>

    It "looks" right but when I run it, I get the "Object Expected" error on the onchange event....

    help!

    thanks a bunch

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Drop down boxes and javascript - How to get the value

    it looks right, but it isn't... first, when you call the onChange event, the function you're calling is expecting a parameter... so you need to supply one... which is where the rest of your problem is, you can't declare a parameter like that... you should be supply a variable name at that point.. and the pass in the value when you call it... hmmm... and your switch statement is wrong... you provided the cases, but not what to check...


    Code:
    function getCompanyFaxAndPhone(companyname) {
      switch (companyname) {
                Case "None":
                    //None Selected
                    document.PayoffEForm.txtPhoneNumber.value = '';
                    document.PayoffEForm.txtFaxNumber.value = '';
                    break;
                Case "Alaska Escrow & Title Insurance":
                    document.PayoffEForm.txtPhoneNumber.value = "somePhone";
                    document.PayoffEForm.txtFaxNumber.value = "someFax";
                    break;
            } // end switch
     } // end function
    Now you can call it like this:

    Code:
    <select id="ddlCompanyName" name="ddlCompanyName" onchange="getCompanyFaxAndPhone(document.getElementById('ddlCompanyName').value)">

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    79

    Re: Drop down boxes and javascript - How to get the value

    *smacks head* I did figure this out this morning before I read your post. Either way, I did make a couple of minor changes based of your ideas. Heres what I came up with:
    1. Create a reference to the control at the BOTTOM of the page(cant reference a control if it hasnt been drawn yet:
    <script>
    var j_TitleCompanyNameRef = document.getElementById('ddlTitleCompanyName');
    </script>
    2. added this to the onchange event of the drop down list:
    onchange="getTitleCompanyFaxAndPhone()"
    3. then rewrote the function, like so:
    <CODE>
    function getTitleCompanyFaxAndPhone() {
    //not all title companies have a standard phone and fax number which is why not all title companies are listed here
    //if a company is selected in the ddl that has no case here, it defaults to the default at the end of the Case statement
    var j_TitleCompanyNameVal = j_TitleCompanyNameRef.options[j_TitleCompanyNameRef.selectedIndex].value;
    switch (j_TitleCompanyNameVal) {
    case "Title Company1":
    document.PayoffEForm.txtPhoneNumber.value = '';
    document.PayoffEForm.txtFaxNumber.value = "321-654-0987";
    break;
    case "Title Company 2":
    document.PayoffEForm.txtPhoneNumber.value = '';
    document.PayoffEForm.txtFaxNumber.value = "123-456-7890";
    break;
    default:
    //alert('Error!');
    document.PayoffEForm.txtPhoneNumber.value = '';
    document.PayoffEForm.txtFaxNumber.value = '';
    break;
    }
    }
    </CODE>
    and POOF! it works!

Posting Permissions

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



Click Here to Expand Forum to Full Width