Set the value of uncheck jquery checkbox

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Set the value of uncheck jquery checkbox



Hi I am new to javascript, html and jquery. I would really appreciate any help or suggestion thanks in advance. I am trying to set the value of jquery checkbox to No if uncheck I adden an html code hidden with the same name of my jquery checkbox before my jquery checkbox


<input type="hidden" name="c1" value="No" />



actually it saving No if uncheck however if it is checked it saving as Yes;No



my jquesry check box


<input grouping="" id="c1" name="c1" type="checkbox" value="Yes" />





do you have 2 inputs with the same name attribute? that wont work
– Alex
Aug 13 at 7:57


name





I only have one checkbox but did create <input type="hidden" name="c1" value="No" /> and it saving no if the checkbox is uncheck however storing and displaying Yes;No if check box is checked
– Aika
Aug 13 at 8:01





You cannot do this using plain HTML. An unchecked checkbox provides no value. The alternative is to do as you have done, by creating a text input with the same name, but then you will have to work with both values.
– Rory McCrossan
Aug 13 at 8:02





exactly. if the checkbox isnt checked, the value wont be submitted. having 2 inputs with the same name doesnt make sense in this case. use jquery or remove one checkbox
– Alex
Aug 13 at 8:03





I also used this <script> $("input[type='checkbox']").on('change', function() $(this).val(this.checked ? "Yes" : "No"); ) </script>
– Aika
Aug 13 at 8:06




3 Answers
3



You can do it like this:


$("#c1").change(function()
var name = $(this).attr("name");
$("input[type=hidden][name=" + name + "]").val(($(this).is(":checked") ? "Yes" : "No"));
console.log($("input[type=hidden][name=" + name + "]").val());
)



Demo




$("#c1").change(function()
var name = $(this).attr("name");
$("input[type=hidden][name=" + name + "]").val(($(this).is(":checked") ? "Yes" : "No"));
console.log($("input[type=hidden][name=" + name + "]").val());
)


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" name="c1" value="No" />
<input grouping="" id="c1" name="c1" type="checkbox" value="Yes" />





always saving No thank you for the help
– Aika
Aug 13 at 8:23





If your using this code, it should switch between yes and no
– Carsten Løvbo Andersen
Aug 13 at 8:32



Use DOM


if($("#c1").checked)
$("#c1").value = "yes";
else
$("#c1").value = "no";



checked return true if checked or false



You can use javascript :


<input onchange="chkboxHandler()" id="c1" name="c1" type="checkbox" value="Yes" checked/>



On value change of checkbox we call the function bellow


<script>
function chkboxHandler()
var chkbox = document.querySelector("input#c1")
if (chkbox.checked)
chkbox.value = "Yes"
else
chkbox.value = "No"


</script>



Hoping you have your answer






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard