How can I set the value of a checkbox field from an event ha

Posted by Rollbase User on 03-Aug-2012 13:06

Here's what we currently have for the onchange() handler for one of our lookup controls (comments added for posting): form.chkInclude.disabled = (1 == this.value); // disable checkbox if 1 entered if (1 == this.value) { form.chkInclude.checked = true; // change the visual state of control chkInclude = true; // change the value of the field } Everything works except changing the value of the Field. In addition to the above, we have tried all of the following: chkInclude.value = true; form.chkInclude.value = true; rbf_SetChecked(chkInclude, true); No matter which of the above formats we attempt, the checkbox is checked and disabled, but the value of the Field remains false, and fails both the client-side and database validation.

All Replies

Posted by Admin on 03-Aug-2012 17:07

Although generic JavaScript control has "value" property in case of Checkbox only "checked" attribute has meaning. Why do you want to set "value" ?

Posted by Admin on 03-Aug-2012 20:18

Edit: if you saw the answer that uses readonly, i remembered that readonly works for inputs only, then I got the code from one of our zones that enables/disables checkboxes which uses onclick and return false.



In the above code, i'm not quite sure with the handling on this line:



form.chkInclude.disabled = (1 == this.value); // disable checkbox if 1 entered



this.value = i'm assuming that this is not the checkbox field itself?



If your having problems saving your checkbox as I assume the code already works above, try changing this :



form.chkInclude.disabled

-- to --

$(form.chkInclude).attr('onclick','return false;'); //using jquery



Additionally, for the handling part, you could change it from :



form.chkInclude.disabled = (1 == this.value); // disable checkbox if 1 entered

-- to --

if (1 == this.value) $(form.chkInclude).attr('onclick','return false;'); //using jquery



As of my experience, when saving a form with a disabled control, the save won't recognize the value inside of it (che

Posted by Admin on 04-Aug-2012 11:34

Try this:



rbf_setChecked("chkInclude", true);



Two problems with original code:

1. Wrong API name (case-sensitive)

2. Field name is a string, not field itself.

Posted by Admin on 04-Aug-2012 11:47

One more thing: when working with client-side JavaScript it is necessary to set your browsers display all JS errors. Should you do that you'd notice wrong API name immediately - and save time.

Posted by Admin on 06-Aug-2012 09:25

The field, whose onchange() event for which this code is meant, contains a list of options for the user. If they select the first option, then the they no longer have a choice for the field represented by the checkbox, it must be set to true. We already have checks in place in the database to prevent bad data, but it is a better user experience if the client can catch the situation and prompt the user appropriately. So for the first option, we want to display the checkbox as disabled and checked, and also set the value of the field to true in memory so that the right value is sent to the database when the user presses the save button.



In my original post the list of other attempts where rbf_setChecked() is mentioned was written from memory for this post. When it was tested in the code, the proper syntax and casing was used. Also, the rbf_setChecked() API is not a solution we can use in this case, as it would change the database before the user presses save.



Martin, how would we restore the default o

Posted by Admin on 06-Aug-2012 09:29

Is the code I gave already working for you guys properly?



to reset, you can use an else clause in the statement i provided above.



else $(form.chkInclude).attr('onclick',''); //using jquery

-- or place it back to your old onclick function if you have one --

else $(form.chkInclude).attr('onclick','checkfunc();'); //using jquery

Posted by Admin on 06-Aug-2012 09:34

I saw that, but checkfunc() looks like another javascript function to me. I meant to reset the onclick() handler to whatever it is if the developer never provides a custom one.

Posted by Admin on 06-Aug-2012 09:36

ahh, you can just clear it if there are no custom ones :



else $(form.chkInclude).attr('onclick',''); //using jquery

This thread is closed