|
-
May 14th, 2013, 01:05 PM
#1
Thread Starter
Lively Member
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
-
May 14th, 2013, 02:27 PM
#2
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
-
May 15th, 2013, 02:35 PM
#3
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|