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>












share|improve this question









New contributor




Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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>












    share|improve this question









    New contributor




    Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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>












      share|improve this question









      New contributor




      Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 10 at 14:31









      Adam Chubbuck

      810217




      810217






      New contributor




      Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 10 at 13:40









      Silverback

      34




      34




      New contributor




      Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Silverback is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          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.






          share|improve this answer








          New contributor




          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Silverback is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          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
































          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.






          share|improve this answer








          New contributor




          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • 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















          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.






          share|improve this answer








          New contributor




          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • 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













          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.






          share|improve this answer








          New contributor




          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          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.







          share|improve this answer








          New contributor




          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 10 at 13:46









          Phantinom

          16




          16




          New contributor




          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Phantinom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • 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




          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










          Silverback is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          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.















           


          draft saved


          draft discarded














          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




















































































          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues