Elike
Elike

Reputation: 25

JavaScript issue opening a new page

I want to navigate to a different page once submit button is clicked. Since I have to do some operations I have to do it via a JavaScript function.

But it's not working, I'm getting the both alerts but it's not navigating to the index.html ( which is in same level as current page), even url is not changing . Can you help me to fix it ?

<html>
    <head>
        <title>Page Title</title>
        <script type="text/javascript">
            function recordPickupDetails1(){
                alert("calling");
                location.href='index.html';
                alert("calling after");
        }
    </script>
</head>
<body> 
    <form onsubmit="recordPickupDetails1()">  
        <ul>
            <li>
                <h2>Pickup Details</h2>
            </li>
            <li>
                <label for="name">Name:</label>
                <input type="text" id="name" name="name" pattern="[A-Za-z ]*"  required />
            </li>
            <li>                
                <button  class="submit" type="submit" > Next </button> 
            </li>
        </ul>
    </form> 
</body>

Upvotes: 1

Views: 46

Answers (3)

patricmj
patricmj

Reputation: 373

Add action to your form. You don`t need javascript.

<form action="index.html">  

http://jsfiddle.net/vm3m74q1/5/

fiddle is updated, so you can use javascript to do something before you submit.

Upvotes: 2

Jacob
Jacob

Reputation: 2767

You need to change location.href = 'index.html' to window.location.href ='index.html'

Here is a JSBin with working code.

http://jsbin.com/giruweqaba/edit?html,js

http://jsbin.com/giruweqaba

Upvotes: 2

Adam
Adam

Reputation: 324

If you want do some operation and then decide if submit should by done or not try:

<script>
function recordPickupDetails1(){

    alert("calling");
    // if you want stop function and submit use:
    return false;
    alert('call2');
}
</script>
<from action="index.htm" onsubmit="return recordPickupDetails1()">

Upvotes: 0

Related Questions