Gform_submit_button Append Javascript example throws error [RESOLVED]

I’m adding the example code found here: https://docs.gravityforms.com/gform_submit_button/#h-4-append-a-javascript-action-to-the-button to my functions.php page on my wordpress site but the code changes throw an error on
$dom->loadHTML( ‘<?xml encoding=“utf-8” ?>’ . $button );
due to an unexpected &.

What am I missing here?

You may have encountered a minor issue with the HTML entities in your code. No worries, I’ve fixed it for you! Just replace the existing code with this one:

add_filter( 'gform_submit_button', 'add_onclick', 10, 2 );

function add_onclick( $button, $form ) {
    $dom = new DOMDocument();
    $dom->loadHTML( '<?xml encoding="utf-8" ?>' . $button );
    $input = $dom->getElementsByTagName( 'input' )->item(0);
    $onclick = $input->getAttribute( 'onclick' );
    $onclick .= " addAdditionalAction('Additional Action');"; // Here's the JS function we're calling on click.
    $input->setAttribute( 'onclick', $onclick );

    return $dom->saveHtml( $input );
}

Copy and paste the code above into your functions.php file, and you should be good to go!

Yeah that fixed my issue, thanks!

The issue here was with the documentation page, which has been updated. Thanks @faisalahammad for bringing that to our attention.

1 Like