• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML Input type checkbox not working properly in HTML.

shivajikobardan

Well-Known Coder
HTML:
<body>
  <form>
    <div>
      <input
        type="checkbox"
        name="mailing-list"
        id="mailing-list"
        value="no"
      />
    </div>

    <div>
      <input type="submit" value="SUBMIT" />
    </div>
  </form>
</body>

Initial value of checkbox is “no”, I click in checkbox and submit, I’m still getting no.

How to fix this issue?
 
Last edited by a moderator:

menator01

King Coder
If you have the value set to no, then that is the value you are sending. Your form has no action. If you submit the form will not do anything.. You should find a tutorial on html forms. It will help you understand the basics.
 

menator01

King Coder
From data-flair-training checkbox attributes are
  • type Specifies the type of input, in this case set as ‘checkbox’.
  • value Specifies the value that will be sent to the server, if the checkbox is checked.
  • name Specifies the name of the control that is delivered to the server.
  • checked Defines a by default checked checkbox. It is a boolean value.
From codeproject.com - If you want to turn the state of the checkbox to yes/no string you have to use some JavaScript like this:
var ans = document.getElementById('checkbox_id').checked ? 'yes' : 'no';
 
Last edited:

Johna

Frontend Web Developer
Staff Team
Guardian
That link you sent is just a localhost link and isn't going to work for anyone, so I've removed it.


It looks like you are thinking that the value of the checkbox is 'no', and when you check it it will turn to 'yes'?

That's not how it works.
If a checkbox is not checked, the form won't send the value of the checkbox.
If it is checked, it'll send the value which is in the value attribute. Your value is 'no' and so you should be expecting to get no, and there's nothing wrong with your code or what you're getting.



Look at The value attribute. I guess the tutorial is wrong then.
What makes you think their value attribute is wrong?
 
Top