Hello
I am trying to create an onchange event that will change a checkbox state when a text box is populated. Below is a simplified version of what I am trying to accomplish. The end result will be a conditional which will show or hide a page based on the state of the check boxes.
I looked at gform_input_change. Not sure if that is the proper function, but I can’t figure out how to adapt my script if it is.
Since modifying the input markup to add an onchange attribute isn’t super easy with Gravity Forms, the best (and probably preferable method anyways) is to use a selector to target the Single Line Text fields and bind the function that unchecks the associated checkbox that way.
Unfortunately this did not work. Or I didn’t know where to place the code. I placed it at the top of the form inside an html element inside script tags.
You should use something like the input event listener to trigger your function. A sample snippet for you below. Should help.
document.addEventListener("input", function (e) {
var currentId = e.target.id;
if (e.target.className === "textarea") { // change this class to your class
var checkbox = document.getElementById('my_checkbox'); // change Id to your id
checkbox.checked = "false";
}
})
Well, the simplest way to do this would be by adding an HTML field to your form and wrapping the snippet with some
<script> </script> // tags
I am a little confused though why you are sharing HTML. Are you using Gravity Forms? Typically, to my knowledge, you can’t simply add onclick events directly to the form elements like you are doing.
The snippet I shared should help you get going. It basically will uncheck a checked checkbox if someone enters a character into your input type = text input.
You can do more with it of course but it should at least help you get started.
You can also open Google Developer Console, specifically “console” to add the above snippet and test. If you add the snippet to the console and hit return, you can then type text into your text input and see if you get the desired behavior.
With the input event listener, there is no need to directly add any onclick events to the HTML aspect of your form. It’s a better practice to not have Javascript inside your HTML. So the event listener “event” will trigger the function similar to the onclick event, except without needing to add JS to the form’s markup itself.