Odess4
Odess4

Reputation: 443

JavaScript - Redirect to a page after radio button submit

I have two radio Buttons in my Page ( Client And Owner ) And i want the user to be redirected to the Client.html page if he chekcs the Client button and redirected to Owner.html if he chekcs the Owner button. Here is my code :

function jump() {
  var select = document.getElementByName("user");
  for (var i = 0; i < selected.length; i++) {
    var value = selected[i].value;
    if (selected[i].checked) {
      if (value === 'Client') {
        window.location.href = 'Client';
      } else {
        window.location.href = 'Owner';
      }
    }
  }
}
<h1>Registration :</h1>
<script src="jump.js"></script>
<form>
  <p>Are you a Client or an Owner ?</p>
  <input type="radio" name="user" value="Client" checked> Client<br>
  <input type="radio" name="user" value="Owner"> Owner<br>
  <input type="button" onclick="jump()" value="OK">
</form>

The other .html Files client and Owner just contain a title. Thank you..

Upvotes: 0

Views: 51

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44125

Your JavaScript code is checking the wrong things - you should try doing this instead:

function hump() {
    var client = document.querySelector("input[value='Client']");
    if (client.checked) {
        window.location.href = "Client.html";
    } else {
        window.location.href = "Owner.html";
    }
}

Upvotes: 0

abney317
abney317

Reputation: 8492

You should be using getElementsByName not getElementByName. Also you named your variable select and are trying to reference it with selected

function jump() {
  var selected = document.getElementsByName("user");
  for (var i = 0; i < selected.length; i++) {
    var value = selected[i].value;
    if (selected[i].checked) {
      if (value === 'Client') {
        window.location.href = 'Client';
      } else {
        window.location.href = 'Owner';
      }
    }
  }
}
<h1>Registration :</h1>
<script src="jump.js"></script>
<form>
  <p>Are you a Client or an Owner ?</p>
  <input type="radio" name="user" value="Client" checked> Client<br>
  <input type="radio" name="user" value="Owner"> Owner<br>
  <input type="button" onclick="jump()" value="OK">
</form>

Upvotes: 2

Related Questions