Javascript help
Discussion
I have the following JS that puts the value of a select list into a text box called SURGEON on a form. It's called from the "onchange = putsur1()" property of the select list sur1
JS starts....
//Added by RB to get info from drop down select lists into text boxes.
function putsur1()
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].value
document.forms[0].SURGEON.value=txt
}
JS ends.....
However, on occasion the value returned is blank, ie they don't have an e-mail address set in MySQL. If the value returned is blank, then I want to put the text of the select list in the box instead.
Any ideas how I would do this? I know I need to test the result of [document.forms[0].sur1.selectedIndex].value to see if it's null and if it is use [document.forms[0].sur1.selectedIndex].text instead, but I'm not familiar with JS to know the syntax and where to insert it.
TAI
JS starts....
//Added by RB to get info from drop down select lists into text boxes.
function putsur1()
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].value
document.forms[0].SURGEON.value=txt
}
JS ends.....
However, on occasion the value returned is blank, ie they don't have an e-mail address set in MySQL. If the value returned is blank, then I want to put the text of the select list in the box instead.
Any ideas how I would do this? I know I need to test the result of [document.forms[0].sur1.selectedIndex].value to see if it's null and if it is use [document.forms[0].sur1.selectedIndex].text instead, but I'm not familiar with JS to know the syntax and where to insert it.
TAI
does this work? Not too familiar with JS and if txt=="" is the correct syntax, might be null.
//Added by RB to get info from drop down select lists into text boxes.
function putsur1()
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].value
if(txt==""
{
txt=document.forms[0].sur1.selectedIndex
}
document.forms[0].SURGEON.value=txt
}
>> Edited by docevi1 on Friday 5th August 15:38
//Added by RB to get info from drop down select lists into text boxes.
function putsur1()
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].value
if(txt==""
{
txt=document.forms[0].sur1.selectedIndex
}
document.forms[0].SURGEON.value=txt
}
>> Edited by docevi1 on Friday 5th August 15:38
Close enough.... And certainly a big help. I ended up with:
JS Starts....
//Added by RB to get info from drop down select lists into text boxes.
function putsur1()
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].value
if(txt==""
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].text
}
document.forms[0].SURGEON.value=txt
}
JS ends....
Thanks
JS Starts....
//Added by RB to get info from drop down select lists into text boxes.
function putsur1()
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].value
if(txt==""
{
txt=document.forms[0].sur1.options[document.forms[0].sur1.selectedIndex].text
}
document.forms[0].SURGEON.value=txt
}
JS ends....
Thanks
I suspect you want something like:
function putsur1()
{
var sur1 = document.forms[0].sur1;
var txt = sur1.options[sur1.selectedIndex].value;
if((txt == null) || (text=="" ))
{
txt = sur1.options[sur1.selectedIndex].text;
}
document.forms[0].SURGEON.value=txt;
}
Edit: beaten to it!
>> Edited by GreenV8S on Friday 5th August 16:01
function putsur1()
{
var sur1 = document.forms[0].sur1;
var txt = sur1.options[sur1.selectedIndex].value;
if((txt == null) || (text=="" ))
{
txt = sur1.options[sur1.selectedIndex].text;
}
document.forms[0].SURGEON.value=txt;
}
Edit: beaten to it!
>> Edited by GreenV8S on Friday 5th August 16:01
DanL said:
BliarOut said:
Interesting... Is || an OR operator in JS?
Yes. Exclusive or, no less, so if one side is true it doesn't bother evaluating the other side...
Dan
[pedant]
I believe thats called a lazy evaluator (dropping out of the test earlier so the right side doesnt get evaluated if the left side is true). The C programming language works the same way.
Exclusive OR would be if both sides are true then the result would be false (true XOR true = false, true OR unknown = true).
scorp said:
[pedant]
I believe thats called a lazy evaluator (dropping out of the test earlier so the right side doesnt get evaluated if the left side is true). The C programming language works the same way.
Exclusive OR would be if both sides are true then the result would be false (true XOR true = false, true OR unknown = true).
Just what I was about to say. Java works the same way too (for ANDs as well). In fact pretty much any well designed language does. No idea about VB though!
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


