Javascript help
Author
Discussion

BliarOut

Original Poster:

72,863 posts

269 months

Friday 5th August 2005
quotequote all
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

docevi1

10,430 posts

278 months

Friday 5th August 2005
quotequote all
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

BliarOut

Original Poster:

72,863 posts

269 months

Friday 5th August 2005
quotequote all
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

GreenV8S

31,023 posts

314 months

Friday 5th August 2005
quotequote all
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

BliarOut

Original Poster:

72,863 posts

269 months

Friday 5th August 2005
quotequote all
Interesting... Is || an OR operator in JS?

DanL

6,587 posts

295 months

Friday 5th August 2005
quotequote all
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

BliarOut

Original Poster:

72,863 posts

269 months

Friday 5th August 2005
quotequote all
Ooh, XOR, nice!

Thanks for the help guys.

scorp

8,783 posts

259 months

Friday 5th August 2005
quotequote all
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).

robbieduncan

1,993 posts

266 months

Friday 5th August 2005
quotequote all
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!