Reputation: 1
<form action="#" method="post" target="_blank">
<div id = "another sub">
</br>
<input type = "password" size = "25px" id="pswd">
</br>
</div>
<button><a href="table.html" target="_blank" onclick="checkAddress('password');" >Submit</a></button>
<script>
function checkAddress(field){
var val = document.getElementById("pswd").value;
if (val === "abcd"){
alert ("Matched");
}
else {
alert("Try for Next Time");
}
}
</script>
</form>
i have a form here in which it first check password than takes to the another page
Upvotes: 0
Views: 1674
Reputation: 1
It would be wise to not use inline js, using a examplename.js file that is loaded would be better solution.
function checkAddress(field) {
var val = document.getElementById("pswd").value;
if (val === "abcd") {
alert("Matched");
document.location.href = 'table.html';
} else {
alert("Try for Next Time");
}
}
<div id="another sub">
<input type="password" size="25px" id="pswd">
</div>
<button onclick="checkAddress();">Submit</button>
Upvotes: 0
Reputation: 435
You have to Add your Script Tag below the form tag or you can add it at the end of the page before the body closing tag, make sure to include the js in the page
<form action="#" method="post" target="_blank">
<div id = "another sub">
</br>
<input type = "password" size = "25px" id="pswd">
</br>
</div>
<button onclick="checkAddress('password');">Submit</button>
</form>
<script>
function checkAddress(field){
var val = document.getElementById("pswd").value;
if (val === "abcd"){
alert ("Redirect To page if Matched Found");
window.location = "http://www.redirecturlhere.com/"
}
else {
alert("Try for Next Time");
}
}
</script>
Upvotes: 2
Reputation: 44125
Don't use a form to avoid the reload:
function checkAddress(field) {
var val = document.getElementById("pswd").value;
if (val == "abcd") {
alert("Matched");
location.href = "table.html";
} else {
alert("Try for Next Time");
}
}
<div id="another sub">
<input type="password" size="25px" id="pswd">
</div>
<button onclick="checkAddress()">Submit</button>
Upvotes: 0
Reputation: 3205
In your case, you don't need <form>
and <a>
tags. Just redirect when password is input correct.
function checkAddress(field) {
var val = document.getElementById("pswd").value;
if (val === "abcd") {
alert("Matched");
document.location.href = 'table.html';
} else {
alert("Try for Next Time");
}
}
<div id="another sub">
<input type="password" size="25px" id="pswd">
</div>
<button onclick="checkAddress();">Submit</button>
Upvotes: 0