I have a script with some variables, & I would like to update one of these variables depending on the checkbox being checked or not. The checkbox is unselected by default. What I have now is this:
var checked = $('#accept').attr('checked');if (checked) { permission = "Y";} else { permission = "N";};var track = { version: 123, id: 123456789, order: { sv1: 'Y', sv2: 'N', sv3: 'Name', sv4: permission, orderid: '123xyz' }};console.log(track);I would like to update the content of the variable “permission” based on a click event of a checkbox:
$('#accept').click(function () { if (checked) { permission = "Y"; } else { permission = "N"; };});But how is that possible?
In order to update your object key (sv4) value:
If you already store the permission into your Object track, well, than don’t hesitate to use your object & directly manipulate it & setting a new value to it’s key using =.
var permission = $('#accept').is(':checked') ? "Y" : "N";var track = { version: 123, id: 123456789, order: { sv1: 'Y', sv2: 'N', sv3: 'Name', sv4: permission, orderid: '123xyz' }};console.log( track.order.sv4 ); // returns: N$('#accept').change(function () { track.order.sv4 = this.checked ? "Y" : "N"; console.log( track.order.sv4 ); // returns: Y});Never forget to define internally your variables by setting var.
The $('#accept').is(':checked') uses the jQuery .is() method that returns a boolean (true/false) which than we set using Ternary Operator ( ? : ) to "Y" / "N"
No comments:
Post a Comment