'Open new page using if else statement to check to see if an html form element is equal to a certain word
How do I open new page using an if else statement to check to see if an html form element is equal to a certain word or phrase. Basically, I have a form and if the user types certain words or phrases (say John or Maria) into, say, the Name input, then I want to write an if, else statement to open a new page, but only when Name is filled out with John or Maria.
Solution 1:[1]
<input id="name" name="person" />
<script>
function opener() {
var text = document.getElementById('name').value;
var targetNames = {
john : "http://example1",
maria : "http://example2"
};
if (text in targetNames) {
window.open(targetNames[text]);
}
}
document.getElementById('name').addEventListener('keyup', opener, false);
</script>
Solution 2:[2]
`<input id="name" name="person" />
<script>
function opener() {
var text = document.getElementById('name').value;
var targetNames = {
john : "http://example1",
maria : "http://example2"
};
if (text in targetNames) {
window.open(targetNames[text]);
}
}
document.getElementById('name').addEventListener('keyup', opener, false);
</script>`
"I hope this helps!"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Zakiah823 |