...

View Full Version : Resolved Don't understand why this IF statement doesn't work



DanielHowden
11-13-2010, 10:00 AM
Ive searched on this board but cant find out why this doesn't work.



<script type="text/javascript">
function showSelected(val)

if (val == "Nil")
{
document.getElementById('JustText').innerHTML='hi'
}
else
{
document.getElementById('selectedResult').innerHTML="<a href='mailto:" + val + "'>" + val + "</a><p>"
}
</script>

Dormilich
11-13-2010, 10:05 AM
what should this code do what it doesn’t do?

glenngv
11-13-2010, 10:20 AM
Are you checking for null value?


if (val == null)

or empty value?


if (val == "")

Either way, you can just do this:

if (!val)

DanielHowden
11-13-2010, 10:40 AM
Sorry for not explaining this better val is a string variable and "Nil" is a srting value. I want to check that if val is = to "Nil" then do something.

No one has pointed out that the syntax is wrong so I wonder if the error lays in the HTML code because I get an error on the page if I run this IF statement but if I take out the IF statement and just run with the following code I have no errors.



<script type="text/javascript">
function showSelected(val)

{
document.getElementById('selectedResult').innerHTML="<a href='mailto:" + val + "'>" + val + "</a><p>"
}
</script>

glenngv
11-13-2010, 10:43 AM
The HTML element with id "JustText" probably does not exist. Maybe you were intending it to mean "selectedResult" and not "JustText"?

DanielHowden
11-13-2010, 10:50 AM
Worked it out. I needed an extra set of {} around the outside of the IF statement. I didn't know I had to have that.



<script type="text/javascript">
function showSelected(val)

{ if (val == "Select User")
{

document.getElementById('selectedResult').innerHTML="Select User"
}
else
{
document.getElementById('selectedResult').innerHTML="<a href='mailto:" + val + "'>" + val + "</a><p>"
}}
</script>

Philip M
11-13-2010, 10:53 AM
What was missing here was function showSelected(val) {

To avoid this sort of probem I suggest that you always place the opening brace { on the same line
as function functioname() {, if (...) { etc, and not on the following line. And do not place two {{ or }} on the same line. Doing that will one day cause you much grief.

glenngv
11-13-2010, 10:55 AM
Oh yeah. I didn't notice that. You need to enclose the function body inside { }


function whatever()
{

//whatever

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum