Dynamically attach onchange script to checkboxes in catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2016 05:59 AM
I'm wondering if this is possible. I would like to detect a change to the value of any of a set of checkboxes, and run a script that validates what/how many are checked in order to determine whether the requirements for the form are meant. I would like to be able to display that visually, rather then the user having to wait until an on-submit for the validation to occur/message to be displayed.
I would like this to be dynamic so that if there are 10 checkboxes, I don't have to write 10 seperate onChange client scripts. I'm thinking one on-load script, with a list of the 10 checkboxes for it to loop through and attach a script or function to them.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2016 06:16 AM
Chris,
If you use documented methods, you will have to create 10 different onChnage client scripts. What you can do is create an onLoad client script and trigger change events on these fields at one place and validate accordingly. Let me know if you need any help in scripting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2016 06:22 AM
I'm not too concerned with it being a 'documented' method at this point. There are some places where the best interface for the user requires DOM manipulation, and you just have to weigh the risk/reward. This would be a commonly applied method in our system and will get thoroughly tested with upgrades.
The only reason I haven't come up with a solution is the checkbox that the user sees on the screen is not the actual input variable. (Geneva)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2016 06:25 AM
I agree. What do you mean by it is not actual input variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2016 06:33 AM
If you look at the DOM for the checkbox that is visually there on the screen, and then you do a g_form.getElement('cb_example'), you'll see that it's actually a different DOM element that is hidden on the screen. Attaching javascript to that hidden element will not get triggered by a user interaction, since it is javascript that changes it. I'm hesitant to change the onclick attached to the visible element, though if i could append a single function to it, that might be enough... trying that now.