radio button switches upon submission
up vote
0
down vote
favorite
So when I select the radio button labeled black and click the add button to display the value, the radio button labeled red get selected and that value is displayed. Heres my code:
function add() {
var total;
if (document.getElementById("btn").checked = true) {
total = 0
} else if (document.getElementById("2ndbtn").checked = true) {
total = 1;
} else {
total = 0
};
document.getElementById("show").innerHTML = total;
}
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<h5>what color is the car?</h5>
<input type="radio" name="q1" value="0" id="btn" /> red
<input type="radio" name="q1" value="0" id="2ndbtn" /> black
<button type="button" onclick="add();">add</button>
<p id="show"></p>
<script src="questions.js"></script>
</body>
</html>
button selection radio
New contributor
add a comment |
up vote
0
down vote
favorite
So when I select the radio button labeled black and click the add button to display the value, the radio button labeled red get selected and that value is displayed. Heres my code:
function add() {
var total;
if (document.getElementById("btn").checked = true) {
total = 0
} else if (document.getElementById("2ndbtn").checked = true) {
total = 1;
} else {
total = 0
};
document.getElementById("show").innerHTML = total;
}
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<h5>what color is the car?</h5>
<input type="radio" name="q1" value="0" id="btn" /> red
<input type="radio" name="q1" value="0" id="2ndbtn" /> black
<button type="button" onclick="add();">add</button>
<p id="show"></p>
<script src="questions.js"></script>
</body>
</html>
button selection radio
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So when I select the radio button labeled black and click the add button to display the value, the radio button labeled red get selected and that value is displayed. Heres my code:
function add() {
var total;
if (document.getElementById("btn").checked = true) {
total = 0
} else if (document.getElementById("2ndbtn").checked = true) {
total = 1;
} else {
total = 0
};
document.getElementById("show").innerHTML = total;
}
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<h5>what color is the car?</h5>
<input type="radio" name="q1" value="0" id="btn" /> red
<input type="radio" name="q1" value="0" id="2ndbtn" /> black
<button type="button" onclick="add();">add</button>
<p id="show"></p>
<script src="questions.js"></script>
</body>
</html>
button selection radio
New contributor
So when I select the radio button labeled black and click the add button to display the value, the radio button labeled red get selected and that value is displayed. Heres my code:
function add() {
var total;
if (document.getElementById("btn").checked = true) {
total = 0
} else if (document.getElementById("2ndbtn").checked = true) {
total = 1;
} else {
total = 0
};
document.getElementById("show").innerHTML = total;
}
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<h5>what color is the car?</h5>
<input type="radio" name="q1" value="0" id="btn" /> red
<input type="radio" name="q1" value="0" id="2ndbtn" /> black
<button type="button" onclick="add();">add</button>
<p id="show"></p>
<script src="questions.js"></script>
</body>
</html>
function add() {
var total;
if (document.getElementById("btn").checked = true) {
total = 0
} else if (document.getElementById("2ndbtn").checked = true) {
total = 1;
} else {
total = 0
};
document.getElementById("show").innerHTML = total;
}
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<h5>what color is the car?</h5>
<input type="radio" name="q1" value="0" id="btn" /> red
<input type="radio" name="q1" value="0" id="2ndbtn" /> black
<button type="button" onclick="add();">add</button>
<p id="show"></p>
<script src="questions.js"></script>
</body>
</html>
function add() {
var total;
if (document.getElementById("btn").checked = true) {
total = 0
} else if (document.getElementById("2ndbtn").checked = true) {
total = 1;
} else {
total = 0
};
document.getElementById("show").innerHTML = total;
}
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<h5>what color is the car?</h5>
<input type="radio" name="q1" value="0" id="btn" /> red
<input type="radio" name="q1" value="0" id="2ndbtn" /> black
<button type="button" onclick="add();">add</button>
<p id="show"></p>
<script src="questions.js"></script>
</body>
</html>
button selection radio
button selection radio
New contributor
New contributor
edited Nov 10 at 14:31
Adam Chubbuck
810217
810217
New contributor
asked Nov 10 at 13:40
Silverback
34
34
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
When you do this in your code:
if(document.getElementById("btn").checked = true){
total = 0
}else if(document.getElementById("2ndbtn").checked = true){
total = 1;
}else{total = 0};
You are actually setting the checked value to true, and not checking if it is true. Therefore you will have to change it to this:
if(document.getElementById("btn").checked){
total = 0
}else if(document.getElementById("2ndbtn").checked){
total = 1;
}else{total = 0};
Then it should work.
New contributor
wow thanks! I thought I tried it that way but obviously I did something wrong I appreciate your help it works just fine now!
– Silverback
Nov 10 at 14:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
When you do this in your code:
if(document.getElementById("btn").checked = true){
total = 0
}else if(document.getElementById("2ndbtn").checked = true){
total = 1;
}else{total = 0};
You are actually setting the checked value to true, and not checking if it is true. Therefore you will have to change it to this:
if(document.getElementById("btn").checked){
total = 0
}else if(document.getElementById("2ndbtn").checked){
total = 1;
}else{total = 0};
Then it should work.
New contributor
wow thanks! I thought I tried it that way but obviously I did something wrong I appreciate your help it works just fine now!
– Silverback
Nov 10 at 14:01
add a comment |
up vote
0
down vote
accepted
When you do this in your code:
if(document.getElementById("btn").checked = true){
total = 0
}else if(document.getElementById("2ndbtn").checked = true){
total = 1;
}else{total = 0};
You are actually setting the checked value to true, and not checking if it is true. Therefore you will have to change it to this:
if(document.getElementById("btn").checked){
total = 0
}else if(document.getElementById("2ndbtn").checked){
total = 1;
}else{total = 0};
Then it should work.
New contributor
wow thanks! I thought I tried it that way but obviously I did something wrong I appreciate your help it works just fine now!
– Silverback
Nov 10 at 14:01
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
When you do this in your code:
if(document.getElementById("btn").checked = true){
total = 0
}else if(document.getElementById("2ndbtn").checked = true){
total = 1;
}else{total = 0};
You are actually setting the checked value to true, and not checking if it is true. Therefore you will have to change it to this:
if(document.getElementById("btn").checked){
total = 0
}else if(document.getElementById("2ndbtn").checked){
total = 1;
}else{total = 0};
Then it should work.
New contributor
When you do this in your code:
if(document.getElementById("btn").checked = true){
total = 0
}else if(document.getElementById("2ndbtn").checked = true){
total = 1;
}else{total = 0};
You are actually setting the checked value to true, and not checking if it is true. Therefore you will have to change it to this:
if(document.getElementById("btn").checked){
total = 0
}else if(document.getElementById("2ndbtn").checked){
total = 1;
}else{total = 0};
Then it should work.
New contributor
New contributor
answered Nov 10 at 13:46
Phantinom
16
16
New contributor
New contributor
wow thanks! I thought I tried it that way but obviously I did something wrong I appreciate your help it works just fine now!
– Silverback
Nov 10 at 14:01
add a comment |
wow thanks! I thought I tried it that way but obviously I did something wrong I appreciate your help it works just fine now!
– Silverback
Nov 10 at 14:01
wow thanks! I thought I tried it that way but obviously I did something wrong I appreciate your help it works just fine now!
– Silverback
Nov 10 at 14:01
wow thanks! I thought I tried it that way but obviously I did something wrong I appreciate your help it works just fine now!
– Silverback
Nov 10 at 14:01
add a comment |
Silverback is a new contributor. Be nice, and check out our Code of Conduct.
Silverback is a new contributor. Be nice, and check out our Code of Conduct.
Silverback is a new contributor. Be nice, and check out our Code of Conduct.
Silverback is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239547%2fradio-button-switches-upon-submission%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password