Link to home
Start Free TrialLog in
Avatar of anshealth
anshealthFlag for United States of America

asked on

Javascript If Else Statement

What is the correct format to write javascript code that has an if else element to it? I have a form that is programmed in Java and each question on the form is broken up into sections. At the end of each section you have many options you can A.go on to next section B.save and exit form or C. write in javascript if there is more than one way to handle the question. An example of this is: If yes, go to next question, if no, jump to section 5. How would I write this portion of code in javascript?
SOLUTION
Avatar of CCSOFlag
CCSOFlag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
Further info on this page: http://www.w3schools.com/js/js_if_else.asp
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Proculopsis
Proculopsis


Here's a sample if else:

  if ( option == "next" ) {
    announce( "You selected 'Go to next section'." );
  } else {
    if ( option == "save" ) {
      announce( "You selected 'Save and Exit'." );
    } else {
      if ( option == "write" ) {
        announce( "You selected 'Write'." );
      }
    }
  }

Open in new window


...but if you have many options then you might want to consider a switch statement:

  switch ( option ) {
    case "next":
      announce( "You selected 'Go to next section'." );
    break;
    case "save":
      announce( "You selected 'Save and Exit'." );
    break;
    case "write":
      announce( "You selected 'Write'." );
    break;
  }

Open in new window


Here is a full working example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26821019.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function () {

  $("input.action").click( action );

});

function action() {
  var option = $(this).attr("id");

  switch ( option ) {
    case "next":
      announce( "You selected 'Go to next section'." );
    break;
    case "save":
      announce( "You selected 'Save and Exit'." );
    break;
    case "write":
      announce( "You selected 'Write'." );
    break;
  }

/* ***********************
  if ( option == "next" ) {
    announce( "You selected 'Go to next section'." );
  } else {
    if ( option == "save" ) {
      announce( "You selected 'Save and Exit'." );
    } else {
      if ( option == "write" ) {
        announce( "You selected 'Write'." );
      }
    }
  }
*********************** */

}

function announce( message ) {
  $("#result").text( message );
}

</script>
</head>
<body>

<input id="next" class="action" type="button" value="Next section" />  
<input id="save" class="action" type="button" value="Save and exit" />  
<input id="write" class="action" type="button" value="Write" />  

<fieldset><legend>Result</legend><span id="result">Click a button...</span></fieldset>

</body>
</html>

Open in new window

Keen, are we?
What about an object like this one ?
The number of each choice is the index of the next question(section)
var questions = [
	{
		"question":"What is your sexe",
		"choices": [{"Male":4}, {"Female":1}]
	},
	{ 
		"question":"Have you ever acouchi??",
		"choices": [{"Yes":2}, {"No":4}]
	},
	{ 
		"question":"How many children?",
		"choices": [{"Less or equal than 2":4}, {"More than 2":3}]
	},
	{ 
		"question":"At the same time?",
		"choices": [{"Yes":4}, {"No":4}]
	},
	{ 
		"question":"Test ended\nDo you want to restart it?",
		"choices": [{"Yes":5}, {"No":null}]
	}
	];

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Assuming our quiet asker use ASP
eh eh @mplungjan
I checked its other posts before ;-) and we can remove the first line and  runat="server" lines 4 and 62, no problem
Avatar of anshealth

ASKER

thx