Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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

Legendary 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:
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.
 
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:
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?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom