Check for checked checkbox validatejs - javascript

I'm using validatejs for my forms validations. How can I check if my checkbox is checked or not? Or any other attribute?
https://validatejs.org/

if you just want the value whether checkbox is checked or not you can do it in plain JavaScript as document.querySelector("input[type='checkbox']").checked or simply using id document.querySelector("#id_of_chkbx").checked

Related

Unable to check and uncheck the dynamically rendered checkboxes in react

I am iterating over the permissions and make checkbox checked if permission included in a predefined values(i.e val) and make checkbox unchecked otherwise, which it did well, but when I click the rendered checkboxes to check or to uncheck it do nothing.
I can't check or uncheck after I rendered my checkboxes. I need your help , thanks.
Your role.permissions is never getting updated, so the checkbox checked attribute stays as its initial value. Are you using controlled components? If so, you can add an onChange event handler to your input to update your role.permissions.
Firstly, the ternary operation in checked={role?.permissions?.includes(val[i]) ? true : false} is redundant and can be simplified to just checked={role?.permissions?.includes(val[i])}, since the value of that property will be either true or false anyway.
Secondly, by setting value= to true or false, you are freezing the checkbox to that value. You can omit that setting, as the value attribute will be submitted according to the checked or unchecked value. Also, typically you assign value=checked or value=unchecked, not true and false.
use map function and useState to control your checkbox values.

Ionic 5, ionic checkbox so late behavior

I tried to bind checked attribute value on ion-checbox but the behavior is a bit late.
On my ts file I have an array variable user_id and on my checkbox list i put an action to fill this array depend on which checkbox is checked, and then verify to make the value of checked attribute:
<ion-checkbox [checked]="user_id.includes(user.id) ? true : false" (click)="AddUser(user)"><ion-checkbox>
the user_idis filled and the value of checked change but the checkbox seem not checked, and when i tried to recheck the checkbox, the value change to false but the checkbox seem checked.
I've put some functionality to make condition (example variable type)if the value is true so the checkboxlist is not shown, and with that functionality, when the value of type switch from false to true, the checkbox work well but the behavior is bit late at the moment when the checbox is checked for the first time
TL;DR
click event is working properly with my checkbox and retrieving data is not consistent
Please refer to Ionic documentation on ion-checkbox
using the (click) to capture the action on a checkbox is wrong.
I suggest you use (ionChange) event to trigger your addUser() function.
With [(checked)] binding, it will create an automatic sync between your actions and the values you save.

Checkbox creation using clone() method of jquery

I my form I have duplicate checkbox like
<input type="checkbox" name="todayDimensionStones[].isIssued" id="isIssued" value="Yes"/>
I am creating another checkbox using above using clone() method using jquery.The checkbox box is created successfully.But when I checked and submit the form containing the newly created checkbox and retrieve the value of newly created checkbox,it seems to be empty ie ''. What I to do solve this problem.If any have an idea ,please share with me
You have to change the attribute name
or you have to add [] at the end of the name.
If you send two inputs with the same name without [] at he end, php gives you the last one only.
If anyone stumbles about this: value attribute is probably missing. Set it manually after cloning:
On the cloned element with some checkboxes do:
$clone.find(':checkbox').each(function() {
$(this).attr('checked','checked')
});
Thats on IE9. See https://bugs.jquery.com/ticket/10550

how do I grab the value of a checkbox using javascript?

I have a javascript that needs to grab the value of a checkbox when its checked and ignore its value when its not checked.
Right now i'm grabbing the value of the checkbox with:
$("input[name='tos']:checked").val()
It works when you check the box and submit the form, but if you uncheck it and resubmit the form the old value of "agree" is given. So i'm wondering how to I grab the value of a checkbox only when its checked?
http://jsfiddle.net/dqy5H/
Here is an example fiddle. Sorry for the bad first response.
Don't forget that unchecked checkboxes don't travel in form submission. Your code is ok, but you may have the old value stored on server side.
A workaround is tht you could have an <input type="hidden"/> that changes its value when you check/uncheck the checkbox, and read this hidden on the server. Or simply, ask if the checkbox arrived to the server, if it didn't it means it was unchecked.
Hope this helps. Cheers
Two things, in order of least to most helpful :)
A checkbox should never return a value of "agree"! I'm hoping you misspoke.
I'm not sure the jQuery selector you used there is actually valid. As the other fellow mentions, it is but it only ever sets the value when it's checked. Oof, my bad. In any case, I'd still recommend giving the checkbox an ID/unique class, and then do:
var checked = $('#toscheckbox').val();
You can add IDs without messing up the form's submission data, and I prefer them - names are so fiddly.
Try giving it an id and calling .is(':checked') like in this article to see if it is checked or not.
http://jquery-howto.blogspot.com/2008/12/how-to-check-if-checkbox-is-checked.html

dynamically set checked checkbox in gridview in javascript

I am having id or values in hidden field. I wanted to set checked=true to checkbox which is in gridview ItemTemplate using javascript only(no jQuery please)
document.getElementById("ID").checked = true/false
all your id's should be unique. however, you can implement it yourself, by traversing all "checkboxes" in page, and checking their "id" attribute. see getElementsByClass which uses same principal.

Categories