We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How to use an IF/ELSE statement in HTML

John500
John500 asked
on
Medium Priority
234 Views
Last Modified: 2012-05-11
Greetings,

I have HTML code and I need to incorporate an IF statement.  The code goes something like this:

....
....
/*  Determine what option the user wants */
<select>
  <option>Option_1
  <option>Option_2
 </select>

....
....
/* Depending on what option, display the appropriate links */

IF (Option_1)
<select>
  <option>html_link_1  
  <option>html_link_2
  <option>html_link_3
</select>

ELSE
<select>
  <option>html_link_4  
  <option>html_link_5
  <option>html_link_6
</select>

This is the basic idea but I know the IF/ELSE is not correct.  What is the syntax and do I need javascript for this?  If so please illustrate.

Also, if the user doesn't work from the top down like he/she should, I need to alert the user that they haven't made an Option selection.

There is no code-behind.  This is straight HTML

Thank you !!

If I use java script I realize it would be similar to this code but I don't know what triggers this code to be executed from the HTML code above:

<script type="text/javascript">
var Opt = OptionVariable
if (Opt = 1)
  {
  /*  don't know what to do here */
  }
else if (Opt = 2)
  {
  /*  don't know what to do here */
  }
else
  {
  document.write("<b>You haven't made an Option selection!</b>");
  }
</script>

Open in new window

Comment
Watch Question

You have to use PHP , DO u have PHP installed if so I can provide example
Sorry I'm wrong , This needs javascript not PHP , sorry for that
Jagadishwor DulalSenior Web Developer
CERTIFIED EXPERT

Commented:
Use javascript chain select so that you can do it:
For your reference please see the pages:

http://bonrouge.com/~chain_select_js
http://www.dhtmlgoodies.com/index.html?whichScript=ajax_chained_select

Author

Commented:
Thanks to both of you.

jagadishdulal - I tried the code you suggested - see the picture below.  

As you can see, it almost does what I want but it complicates matters with all the extra stuff.  Is there any way you can modify the code to do what I want.  The selections in my routine are just a few in either case

IF (Option_1)
<select>
  <option>html_link_1  
  <option>html_link_2
  <option>html_link_3
</select>

ELSE
<select>
  <option>html_link_4  
  <option>html_link_5
  <option>html_link_6
</select>

The example is not making it easier.  Thanks again for the help!




html-example.JPG
Senior Web Developer
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Terrific !

That's the ticket.

Thanks!

Author

Commented:
Please see the following question which deals with this code:

https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/HTML/Q_26979239.html

Thanks !
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.