Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    50
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Don't understand why this IF statement doesn't work

    Ive searched on this board but cant find out why this doesn't work.

    Code:
    <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>
    Last edited by DanielHowden; 11-14-2010 at 02:53 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,394
    Thanks
    13
    Thanked 353 Times in 349 Posts
    what should this code do what it doesn’t do?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Are you checking for null value?

    Code:
    if (val == null)
    or empty value?

    Code:
    if (val == "")
    Either way, you can just do this:
    Code:
    if (!val)
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    50
    Thanks
    16
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <script type="text/javascript">
    function showSelected(val)
    	
    	{
    		document.getElementById('selectedResult').innerHTML="<a href='mailto:" + val + "'>" + val + "</a><p>"
    	}
    </script>

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The HTML element with id "JustText" probably does not exist. Maybe you were intending it to mean "selectedResult" and not "JustText"?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New Coder
    Join Date
    Nov 2010
    Posts
    50
    Thanks
    16
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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>

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.
    Last edited by Philip M; 11-13-2010 at 10:58 AM.

  • Users who have thanked Philip M for this post:

    DanielHowden (11-13-2010)

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Oh yeah. I didn't notice that. You need to enclose the function body inside { }

    Code:
    function whatever()
    {
        
         //whatever
      
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    DanielHowden (11-13-2010)


  •  

    Posting Permissions

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