Jump to content

Drop down if..else not working.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Nemesis4895

Nemesis4895

    Newbie

  • Members
  • Pip
  • 9 posts
Hey, I'm new to this site and new to programming in general. Working on basics of javascript currently. Below is the script I'm working on, but its not working. The code looks right to me, but obviously it's wrong. Point me in the right direction please?

<html>

<head>

<script type="text/javascript">

function getIndex()

{

  var x=document.getElementById("mySelect");

  if (x.selectedIndex == 2);

   {

    alert("correct");

   }

  else

   {

    alert("incorrect");

   }

}

</script>

</head>

<body>

<form>

Select your favorite fruit:

<select id="mySelect">

  <option>Apple</option>

  <option>Orange</option>

  <option>Pineapple</option>

  <option>Banana</option>

</select>

<input type=submit value="Go" onclick="getIndex()">

</form>

</body>

</html>



#2
ethikz

ethikz

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts
Are you using IE? I copied the script and tried it in Firefox and it works fine.

#3
Nemesis4895

Nemesis4895

    Newbie

  • Members
  • Pip
  • 9 posts
Nope, I'm using the latest version of firefox. Whenever I click submit, it just refreshes the page.

#4
ethikz

ethikz

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts
Ahh ok I got you now...yea it does that on mine also...hmm i'll look more into it

#5
Nemesis4895

Nemesis4895

    Newbie

  • Members
  • Pip
  • 9 posts
Alright, thanks.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
It has an incorrect ';' after the if-line.

if (x.selectedIndex == 2)[COLOR="red"][SIZE="5"];[/SIZE][/COLOR]