www.webdeveloper.com
Results 1 to 3 of 3

Thread: Stuck on very easy JavaScript Calculate Function

  1. #1
    Join Date
    Mar 2011
    Posts
    6

    Stuck on very easy JavaScript Calculate Function

    Hi. I've been creating a very simple test HTML form that utilizes javascript. All I have on the form is a dropdown, with three options: 1, 2, and 3. Then, I have a submit button. When pressed, a message is supposed to be displayed to the screen saying "You selected option1!" etc.

    However, what happens, is that it loops through everything, so I see:
    "You selected option1"
    "You selected option2"
    "You selected option3"

    I don't understand why it is looping. I don't use any looping functions. My code below is:

    <form name="form1">
    <select name="dropdown1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>

    <input type="button" value="Submit" onClick="calculate()">
    </form>

    <script>
    function calculate()
    {

    var audience;
    audience=document.form1.dropdown1.options[document.form1.dropdown1.selectedIndex].value;

    if(audience="1")
    {
    window.alert("You have selected option " + audience);
    }

    if(audience="2")
    {
    window.alert("You have selected option " + audience");
    }

    if(audience="3")
    {
    window.alert("You have selected option " + audience");
    }

    }


    </script>


    I'm completely baffled as to why it goes through every if statement and generates each message, if someone could help lend a hand.

    Thanks,
    Mikey

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    Change each if statement to:
    Code:
    if (audience == "1") ...
    
    if (audience == "2") ...
    
    if (audience == "3") ...

  3. #3
    Join Date
    Mar 2011
    Posts
    6
    I can't believe that was it... Haha. I feel like such a dumb@ss now. Thanks for your help, though!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles