1

I am trying tot get my javascript code to redirect to the Index.html page if the variable money is equal to 100. Here is the html form code:

<form id = "form"  action = "">
    <h1> Enter the amount of money you would like to spend on your next trip. </h1><br>

            <div class="input-group">
              <span class="input-group-addon">$</span>
              <input type="text" name="money" class="form-control">

            </div>
            <br>
            <button type="submit" class="btn btn-default" onclick = "MoneyForTrip()">Show Trips</button>

            </form>

and the javascript:

var money;

function MoneyForTrip()
{
money = parseInt(
        document.getElementById('form').money.value);

    if (money == 100)
    {
        window.location="Index.html";
    }


    else
    {
        window.alert("Enter another number"); 
    }

}

Any idea how I can get it to redirect to Index.html if money is equal to 100?

Godin1
  • 71
  • 1
  • 11

3 Answers3

1

Try this.

window.location.replace("Index.html")

or

window.location.href = "Index.html"

Also take a look at this Stack Answer.

Community
  • 1
  • 1
j0hnstew
  • 709
  • 8
  • 25
  • Any idea if I should be leaving the action = "" in the form section? It only seems to work if I change this action to Index.html but then it will go to the page whether money == 100 or not. – Godin1 Nov 13 '13 at 02:04
  • if you set action in the form it will go there everytime regardless of if money == 100 – j0hnstew Nov 13 '13 at 03:40
1

Use the submit event instead of button's click event:

var money;
var form = document.getElementById('form');
form.onsubmit = function(e) {
    e.preventDefault();
    money = parseInt(document.getElementById('form').money.value);

    if (money == 100){
        window.location="Index.html";
    }

    else{
        window.alert("Enter another number"); 
    }
}

Demo: http://jsfiddle.net/xzjW3/

Carlangueitor
  • 425
  • 2
  • 15
0

try
<input type="text" name="money" id="moneyfortrip" class="form-control">

var money;

function MoneyForTrip()
{
money = parseInt(
        document.getElementById('moneyfortrip').value);

    if (money == 100)
    {
        window.location="Index.html";
    }


    else
    {
        window.alert("Enter another number"); 
    }

}
Vinh
  • 73
  • 6