Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:56:51+00:00 2026-06-04T02:56:51+00:00

I am trying to create a simple way to force a single checkbox to

  • 0

I am trying to create a simple way to force a single checkbox to display “No” in a form submission if left unchecked, or clicked off. I’ve tried a number of ways and this is the closest I can get, but it still isn’t working properly.

Any help is much appreciated.
Matt

$(function opt() {
        if ($("#optIn").is(":checked")) {
            $("#optIn").value="Yes"
            $("#optIn").click(function() { this.value="No" })
        } else {
            $("#optIn").value="No"
            $("#optIn").click(function() { this.value="Yes" })
        }
    return false
});

opt();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:56:52+00:00Added an answer on June 4, 2026 at 2:56 am

    So to start out quite frankly, I’m not sure where you saw that sort of function setup before. You’re code, as-is, could be reduced to this:

    $(function() { // on DOM ready (when the DOM is finished loading)
        $('#optIn').click(function() { // when the checkbox is clicked
            var checked = $('#optIn').is(':checked'); // check the state
            $('#optIn').val(checked ? "Yes" : "No"); // set the value
        });
        $('#optIn').triggerHandler("click"); // initialize the value
    });
    

    The value of the checkbox, however, is never displayed on the screen. You may need to update a separate field with the values “Yes” or “No”, such as:

    <input type="checkbox" id="optIn" />
    <span id="optInLabel"/>No</span>
    

    And the script:

    $(function() { // on DOM ready (when the DOM is finished loading)
        $('#optIn').click(function() {
            optIn(this);
        });
        optIn($('#optIn')[0]);
    });
    
    function optIn(el) {
        var checked = $(el).is(':checked'); // check the state
        $('#optInLabel').html(checked ? "Yes" : "No"); // set the value
    }
    

    EDIT: Working jsFiddle

    If you need to check whether the box is checked on the server-side post-form-submission, then you could also update a hidden input field with the value “Yes” or “No” and ignore the submitted checkbox element value (as jaredhoyt mentioned in his answer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to come up with a simple and efficient way to create a
Im trying to create a simple input box with form validation, but im not
I'm trying to find a way to create a simple outer join without too
I'm trying to create a single page form to create a 'work item'. One
I'm trying to create a simple layout but can't find a propper way to
I'm trying to create a simple web filtering app in python. The way I
I'm trying to create a simple way of serving downloadable content with Django. The
I'm studying webrat and cucumber and trying to create simple example. Here is my
Im trying to create a simple pan and zoom app using silverlight 4, but
I'm trying to create a simple HTML layout where there is a header, content

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.