DOM Quiz will give incorrect answers - javascript

I am learning DOM and wanted to create a simple JavaScript with Html quiz (for exercise). Now the problem I'm having is that when I hit submit, all of the answers are right instead of one being right and 3 being wrong. I think it is a problem with my html and the way I assigned the ID's to the different tags but I cannot figure out what I am doing wrong.
Code
HTML
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="quiz.css">
</head>
<body>
<div class="QuestionOne">
<form id="quizForm">
<h1> What is your favorite color?</h1>
<input type="radio" id="red" name="color" value="red">
<label for="red">Red</label><br>
<p></p>
<input type="radio" id="blue" name="color" value="blue">
<label for="blue">Blue</label><br>
<p></p>
<input type="radio" id="green" name="color" value="green">
<label for="green">Green</label>
<p></p>
<input type="submit" id="submit" name="color" value="Submit"><br>
</form>
</div>
<script src="quiz.js">
</script>
</body>
</html>
JavaScript
quizForm.addEventListener("submit",function(event) {
event.preventDefault();
var grabAnswer = document.getElementById('red')
console.log(grabAnswer.id);
if (grabAnswer.id == 'red') {
console.log('correct!');
}else{
console.log('wrong');
}
})
Thanks.

You can do this two ways
get the selected value and see if it's correct
get the correct answer and see if it's selected
The existing answer handles (1) so here's the solution for the other option.
Taking your original code, change
if (grabAnswer.id == 'red') {
to
if (grabAnswer.checked) {
(where grabAnswer is document.getElementById('red'))
quizForm.addEventListener("submit", function(event) {
event.preventDefault();
// get the correct answer
var grabAnswer = document.getElementById('red')
// see if it's been selected
if (grabAnswer.checked) {
console.log('correct!');
} else {
console.log('wrong');
}
})
<div class="QuestionOne">
<form id="quizForm">
<h1> What is your favorite color?</h1>
<input type="radio" id="red" name="color" value="red">
<label for="red">Red</label><br>
<p></p>
<input type="radio" id="blue" name="color" value="blue">
<label for="blue">Blue</label><br>
<p></p>
<input type="radio" id="green" name="color" value="green">
<label for="green">Green</label>
<p></p>
<input type="submit" id="submit" name="color" value="Submit"><br>
</form>
</div>

Problem is that you always take the red in your grapAnswer, but you have to use var grabAnswer = document.querySelector('input[name="color"]:checked');
Demo
var quizForm = document.getElementById("quizForm")
quizForm.addEventListener("submit", function(event) {
event.preventDefault();
var grabAnswer = document.querySelector('input[name="color"]:checked');
console.log(grabAnswer.id);
if (grabAnswer.id == 'red') {
console.log('correct!');
} else {
console.log('wrong');
}
})
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="quiz.css">
</head>
<body>
<div class="QuestionOne">
<form id="quizForm">
<h1> What is your favorite color?</h1>
<input type="radio" id="red" name="color" value="red">
<label for="red">Red</label><br>
<p></p>
<input type="radio" id="blue" name="color" value="blue">
<label for="blue">Blue</label><br>
<p></p>
<input type="radio" id="green" name="color" value="green">
<label for="green">Green</label>
<p></p>
<input type="submit" id="submit" name="color" value="Submit"><br>
</form>
</div>
<script src="quiz.js">
</script>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>Page Title</title>
</head>
<body>
<h1> What is your favorite color?</h1>
<form id="myForm">
<input type="radio" id="red" name="color" value="red">
<label for="red">Red</label><br>
<p></p>
<input type="radio" id="blue" name="color" value="blue">
<label for="blue">Blue</label><br>
<p></p>
<input type="radio" id="green" name="color" value="green">
<label for="green">Green</label>
<p></p>
<button type="button" id="button">Submit</button><br>
</form>
</body>
</html>
<script>
$(document).ready(function() {
$("#button").click(function() {
if($('input[name=color]:checked', '#myForm').val()=='red')
{
alert('Succesfully!');
}
else
{
alert("No");
}
});
});
</script>
Can you try this code?

Related

How to create a jquery script for counting the number of checkboxes ticked

[2]. So far I have written the following code:-
<!DOCTYPE html>
<html lang="en">
<head>
<title>Check Box using jQuery</title>
</head>
<body>
<form id="form">
<input type="checkbox" id="red" name="red" value="Red"/>Red
<input type="checkbox" id="green" name="green" value="Green"/>Green
<input type="checkbox" id="blue" name="blue" value=" Blue"/>Blue
<input type="checkbox" id="black" name="black" value="Black"/>Black<br/>
</form>
<div id="result">0 boxes are checked</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="chkbox.js"></script>
</body>
</html>
And now the script which I have written in the chkbox.js file is:-
$(document).ready(function(){
var $checkboxes=$('#form td input[type="checkbox"]');
$checkboxes.change(function(){
var ccc=$checkboxes.filter(':checked').length;
$('span').text(ccc);
});
});
I unable to figure out why this code is not working.
First, you do not have td element inside the #form element, 2nd you're printing inside a generic span that doesn't exist. You could do something like this:
<form id="form">
<input type="checkbox" id="red" name="red" value="Red" /> Red
<input type="checkbox" id="green" name="green" value="Green" /> Green
<input type="checkbox" id="blue" name="blue" value=" Blue" /> Blue
<input type="checkbox" id="black" name="black" value="Black" /> Black
</form>
<div id="result">No boxes are checked</div>
And for your javascript like this:
$(document).ready(function() {
var $checkboxes = $('#form input[type="checkbox"]');
$checkboxes.change(function() {
var nr = $('#form input:checked').length;
var message = (nr === 0)?'No':nr;
message += (nr === 1)?" box is checked":" boxes are checked";
$('#result').text(message);
});
});
But, in general, study more about jquery, javascript and html.
You don't have any tds or spans in your code , check this simple way
$('#form input.checkbox').click(function(){
$('#result').text($('#form input.checkbox:checked').length);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="form">
<input type="checkbox" class="checkbox" id="red" name="red" value="Red"/>Red
<input type="checkbox" class="checkbox" id="green" name="green" value="Green"/>Green
<input type="checkbox" class="checkbox" id="blue" name="blue" value=" Blue"/>Blue
<input type="checkbox" class="checkbox" id="black" name="black" value="Black"/>Black<br/>
</form>
<div><strong id="result">0</strong> boxes are checked</div>

How can I Validate my form using class only?

I have radio buttons that has a same class.
and this radio buttons is dynamic, that's why I am thinking on using the class for validation.
Here is my code.
$('#listening_choices_wrapper').on('submit', function(e) {
e.preventDefault();
$(".validation_radio").each(function() {
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<form id="listening_choices_wrapper">
<div class="listening_question_scenario">
<p id="display_question_scenario">
1. Question 1?
</p>
</div>
<div id="question_choices" class="listening_question_choice">
<input type="radio" class="validation_radio" name="answer_choice_0" value="17">
<label>Test 1</label>
<input type="radio" class="validation_radio" name="answer_choice_0" value="18">
<label>Test 2</label>
<input type="radio" class="validation_radio" name="answer_choice_0" value="19">
<label>Test 3</label>
<input type="radio" class="validation_radio" name="answer_choice_0" value="20">
<label>Test 4</label>
</div>
<div class="listening_question_scenario">
<p id="display_question_scenario">
2. Question 2?
</p>
</div>
<div id="question_choices" class="listening_question_choice">
<input type="radio" class="validation_radio" name="answer_choice_1" value="17">
<label>Test 5</label>
<input type="radio" class="validation_radio" name="answer_choice_1" value="18">
<label>Test 6</label>
<input type="radio" class="validation_radio" name="answer_choice_1" value="19">
<label>Test 7</label>
<input type="radio" class="validation_radio" name="answer_choice_1" value="20">
<label>Test 8</label>
</div>
<button type="submit" class="btn">Submit</button>
</form>
</body>
</html>
How can I validate them? so that the user can't submit it.
Any help would be really appreciated.
You can add the required attribute to all the input tags for native browser validation:
<div id="question_choices" class="listening_question_choice">
<input type="radio" class="validation_radio" name="answer_choice_0" value="17" required>
<label>Test 1</label>
<input type="radio" class="validation_radio" name="answer_choice_0" value="18" required>
<label>Test 2</label>
<input type="radio" class="validation_radio" name="answer_choice_0" value="19" required>
<label>Test 3</label>
<input type="radio" class="validation_radio" name="answer_choice_0" value="20" required>
<label>Test 4</label>
</div>
But to answer your question, I wrote this jQuery script:
$('#listening_choices_wrapper').on('submit', function(e) {
$('.error').remove()
e.preventDefault();
$('.listening_question_choice').each((i, e) => {
let valid = false
$(e).find('.validation_radio').each((i ,e) => {
if($(e).prop("checked")) {
valid = true
}
})
if (!valid) {
$(e).append('<div class="error" style="color: red;">Error: this radio is required!</div>')
}
})
});
I hope this helps!
I would suggest that you wrap each bank of answers in a fieldset element, and put the class on that. You can then iterate over those with each (as you did in your code) and look to see whether there are no input elements that are :checked
if($(this).find("input:checked").length == 0){
// no answer given
}
You can then also use some sort of label to improve the message to the user - and if any answers missed only then call e.preventDefault().
Live example below:
$('#listening_choices_wrapper').on('submit', function(e) {
var missingAnswers = [];
$(".validation_radio").each(function() {
if($(this).find("input:checked").length == 0){
missingAnswers.push($(this).data("label"));
}
});
if(missingAnswers.length > 0){
e.preventDefault();
alert(`You are missing answers to: ${missingAnswers}`)
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<form id="listening_choices_wrapper">
<div class="listening_question_scenario">
<p id="display_question_scenario">
1. Question 1?
</p>
</div>
<div id="question_choices" class="listening_question_choice">
<fieldset class="validation_radio" data-label="answer1">
<input type="radio" name="answer_choice_0" value="17">
<label>Test 1</label>
<input type="radio" name="answer_choice_0" value="18">
<label>Test 2</label>
<input type="radio" name="answer_choice_0" value="19">
<label>Test 3</label>
<input type="radio" name="answer_choice_0" value="20">
<label>Test 4</label>
</fieldset>
</div>
<div class="listening_question_scenario">
<p id="display_question_scenario">
2. Question 2?
</p>
</div>
<div id="question_choices" class="listening_question_choice">
<fieldset class="validation_radio" data-label="answer2">
<input type="radio" name="answer_choice_1" value="17">
<label>Test 5</label>
<input type="radio" name="answer_choice_1" value="18">
<label>Test 6</label>
<input type="radio" name="answer_choice_1" value="19">
<label>Test 7</label>
<input type="radio" name="answer_choice_1" value="20">
<label>Test 8</label>
</fieldset>
</div>
<button type="submit" class="btn">Submit</button>
</form>
</body>
</html>

Redirect to value url on select submit

I am looking to redirect the user to the URL specified in the select value. For styling to the select I had to go a custom route hence the way its set-up. Am I approaching it wrong, if so what is the most efficient way to approach it?
HTML
<form>
<span class="dropdown-el" onChange="window.document.location.href=this.options[this.selectedIndex].value;">
<input type="radio" name="services" value="Relevance" checked="checked" id="services">
<label for="services">Select one ...</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-best"><label for="sort-best">Option 1</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-low"><label for="sort-low">Option 2</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-high"><label for="sort-high">Option 3</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-brand"><label for="sort-brand">Option 4</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-name"><label for="sort-name">Option 5</label>
</span>
<button type="submit" class="hero-form-btn" onClick="WinOpen();">Go →</button>
</form>
Jquery/JavaScript
<script>
jQuery(function($) {
$('span').on('change', function() {
var url = $(this).val();
if (url) {
window.location = url;
}
return false;
});
});
</script>
I've looked through it so many times that I'm beginning to get lost. Any help is much appreciated.
You should take the value from the selected radio button:
jQuery(function($) {
$('span').on('change', function(val) {
var url = $("input[name='services']:checked").val();
if (url) {
window.location = url;
}
return false;
});
});
You can test it here:
https://jsfiddle.net/6k8cnxhp/
What is the purpose of submitting and what actually happens besides redirecting the user?
If it just for redirect, change the button type to button instead of submit since when you hit submit you refresh the page and any code assign to the function (winOpen) won't be execute...
If you don't need to actually submit the form, your code can be something like that:
let url = document.querySelectorAll('input[name="services"]');
for (let i=0; i < url.length; i++) {
url[i].addEventListener('change', function() {
document.querySelectorAll('form > button')[0].setAttribute('onclick', 'window.location.assign("'+url[i].value+'")');
});
};
<form>
<span class="dropdown-el" onChange="window.document.location.href=this.options[this.selectedIndex].value;">
<input type="radio" name="services" value="Relevance" checked="checked" id="services">
<label for="services">Select one ...</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-best"><label for="sort-best">Option 1</label>
<input type="radio" name="services" value="http://www.microsoft.com" id="sort-low"><label for="sort-low">Option 2</label>
<input type="radio" name="services" value="http://www.stackoverflow.com" id="sort-high"><label for="sort-high">Option 3</label>
<input type="radio" name="services" value="http://www.mdn.com" id="sort-brand"><label for="sort-brand">Option 4</label>
<input type="radio" name="services" value="http://www.cnn.com" id="sort-name"><label for="sort-name">Option 5</label>
</span>
<button type="button" class="hero-form-btn">Go →</button>
</form>
Your button is in the form tag with a type="submit". Submit adds http://yoursite.com?yourQueryToServer the query string to your URL to be sent to the server.
If you want to leave the button in the form tag, change the type="button"
Try this.
I didn't quite understand why you have a submit button, but you reload onChange, so I changed that.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<form>
<span class="dropdown-el" onChange="window.document.location.href=this.options[this.selectedIndex].value;">
<input type="radio" name="services" value="Relevance" checked="checked" id="services">
<label for="services">Select one ...</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-best"><label
for="sort-best">Option 1</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-low"><label for="sort-low">Option
2</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-high"><label
for="sort-high">Option 3</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-brand"><label
for="sort-brand">Option 4</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-name"><label
for="sort-name">Option 5</label>
</span>
<button type="button" class="hero-form-btn" onClick="WinOpen();">Go →</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>
$('span').on('change', function (val) {
let url = $("input[name='services']:checked").val();
if (url) {
window.location = url;
}
});
function changePage() {
window.location = url;
}
</script>
</body>
</html>
or with reloading onChange
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<form>
<span class="dropdown-el" onChange="window.document.location.href=this.options[this.selectedIndex].value;">
<input type="radio" name="services" value="Relevance" checked="checked" id="services">
<label for="services">Select one ...</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-best"><label
for="sort-best">Option 1</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-low"><label for="sort-low">Option
2</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-high"><label
for="sort-high">Option 3</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-brand"><label
for="sort-brand">Option 4</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-name"><label
for="sort-name">Option 5</label>
</span>
<button type="button" class="hero-form-btn" onClick="WinOpen();">Go →</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>
var url = null
jQuery(function ($) {
$('span').on('change', function (val) {
var url = $("input[name='services']:checked").val();
if (url) {
window.location = url;
}
return false;
});
});
function changePage() {
window.location = url;
}
</script>
</body>
</html>
o button click option place windowlocation.href I modify the code <script>
var url = null
jQuery(function ($) {
// $('span').on('change', function (val) {
$('.hero-form-btn').on('click',function(e){
var url = $("input[name='services']:checked").val();
if (url) {
window.document.location.href = url;
//window.location = url;
}
//});
return false;
});
});
function changePage() {
window.location = url;
}
</script>

Append radio button

How to append radio button using jquery or javascript to save into the databse?
I appended input types like text,textarea,checkbox,select with options.
My code is
<!DOCTYPE html>
<html lang="en">
<head>
<title>
apnd
</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]">Gender<input type="radio" name="gender[]" value="M">Male<input type="radio" name="gender[]" value="F">FemaleAddress<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
$('.add_field').click(function(){
$('.wrapper').append(htmldata);
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove();
});
});
</script>
</head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
Name<input type="text" name="cname[]">
Mobile Number<input type="text" name="mob[]">
Gender<input type="radio" name="gender[]" value="M">Male
<input type="radio" name="gender[]" value="F">Female
Address<textarea name="address[]"></textarea>
<input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
</html>
using php I want to save the values to database.
$name=sc_sql_injection($_POST['cname']);
$mobile=sc_sql_injection($_POST['mob']);
$gender=sc_sql_injection($_POST['gender']);
$address=sc_sql_injection($_POST['address']);
$count=count($_POST['cname']);
for($i=0;$i<$count;$i++)
{
$sql="insert into t2(name,mobile,gender,address) values('$name[$i]','$mobile[$i]','$gender[$i]','$address[$i]')";
sc_exec_sql($sql,$con);
}
Update
Append radio button
<!DOCTYPE html>
<html lang="en">
<head>
<title>
apnd
</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var i=1;
$('.add_field').click(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]"> Gender<input type="radio" name="gender['+i+']" value="M">Male<input type="radio" name="gender['+i+']" value="F">Female Address<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
i++;
$('.wrapper').append(htmldata);
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove();
i--;
});
});
</script>
</head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
Name<input type="text" name="cname[]">
Mobile Number<input type="text" name="mob[]">
Gender<input type="radio" name="gender[0]" value="M">Male
<input type="radio" name="gender[0]" value="F">Female
Address<textarea name="address[]"></textarea>
<input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
</html>
$(document).ready(function(){
var i=1;
$('.add_field').click(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]"> Gender<input type="radio" name="gender['+i+']" value="M">Male<input type="radio" name="gender['+i+']" value="F">Female Address<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
i++;
$('.wrapper').append(htmldata);
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove();
i--;
});
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
Name<input type="text" name="cname[]">
Mobile Number<input type="text" name="mob[]">
Gender<input type="radio" name="gender[0]" value="M">Male
<input type="radio" name="gender[0]" value="F">Female
Address<textarea name="address[]"></textarea>
<input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
</html>

HTML page with Radio Buttons

I'm trying to figure out how to get my radio buttons to direct me to the webpage after you make your selection and hit submit. This is the code I have thus far.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Radio Buttons</h1>
</div>
<div data-role="main" class="ui-content">
<form method="post" action="demoform.asp">
<fieldset data-role="controlgroup">
<legend>Choose a Website:</legend>
<label for="ESPN.com">ESPN.com</label>
<input type="radio" name="website" id="ESPN.com" value="http://espn.go.com">
<label for="Facebook.com">Facebook.com</label>
<input type="radio" name="website" id="Facebook.com" value="https://www.facebook.com">
<label for="Apple.com">Apple.com</label>
<input type="radio" name="website" id="Apple.com" value="http://www.apple.com">
</fieldset>
<input type="submit" data-inline="true" value="Submit">
</form>
</div>
</div>
</body>
</html>
You should add the event onclick = "document.location.href='somepage.htm'" inside your input radio tag
You can use JQuery like this:
$('input:radio[name="website"]').change(
function(){
if ($(this).is(':checked') && $(this).val() == '1') {
window.location = "http://espn.go.com";
}
else if ($(this).is(':checked') && $(this).val() == '2'){
window.location = "https://www.facebook.com";
}
else if ($(this).is(':checked') && $(this).val() == '3'){
window.location = "http://www.apple.com";
}
});
HTML Radio Buttons
<input type="radio" name="website" id="ESPN.com" value="1">
<label for="Facebook.com">Facebook.com</label>
<input type="radio" name="website" id="Facebook.com" value="2">
<label for="Apple.com">Apple.com</label>
<input type="radio" name="website" id="Apple.com" value="3">
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function whichsite(form){
var sites = form.elements.site, i = sites.length;
while (--i > -1){
if(sites[i].checked){
return sites[i].value;
}
}
}
</script>
</head>
<body>
<form action="#" onsubmit="window.open(whichsite(this), '_blank'); return false;">
<b>Where do you need to go?:</b>
<p>
<label><input type="radio" name="site" value="http://yahoo.com/">Yahoo</label>
<label><input type="radio" name="site" value="http://google.com/">Google</label>
<label><input type="radio" name="site" value="http://amazon.com/">Amazon</label>
<label><input type="radio" name="site" value="http://cnn.com/" checked>CNN</label>
<p>
<input type="submit" value="Submit">
</form>
</body>
</html>

Categories