How to switch the submit button from an input element to a button element
SOLVE
I recently was asked to add an arrow to our submit buttons for design purposes. We're embedding Hubspot forms into a Drupal website, and I struggled to find a way to do this using the default input element - psuedo-elements and inserted HTML don't work and trying to get something added outside of the input and then overlaying it with CSS seemed messy. I didn't see a similar post here about this, so I figured this might help other folks.
In the end, I used the following code to simply change the <input> to a <button> and it seems to work properly for me. I see submissions come in as expected, and the client is happy they have their arrow on the button.
Without removing the original "with-arrow" class, the script was re-targeting the elements and removing the button text for some reason.
If you're actually using Drupal, this code was used in the hubspot-form.html.twig template that the hubspot_forms module provides, but this should work for any system where you're embedding the forms yourself.
<script> hbspt.forms.create({ css: '', target: '#{{ target }}', portalId: '{{ portal_id }}', formId: '{{ form_id }}', submitButtonClass: 'btn btn-primary btn-sm with-arrow', onFormReady: function($form){ // Swap the Submit input element for a button element so we can style it. // Get a reference to the input element var inputElements = document.getElementsByClassName('with-arrow'); for ( var i = 0; i < inputElements.length; i++) {
// Create a new button element var buttonElement = document.createElement('button');
// Copy the relevant properties and attributes from the input to the button buttonElement.className = inputElements[i].className; // Change the arrow class so we don't re-target the button element, and // add the proper arrow class. buttonElement.classList.remove('with-arrow'); buttonElement.classList.add('btn--with-arrow'); buttonElement.type = inputElements[i].type; buttonElement.textContent = inputElements[i].value; buttonElement.onclick = inputElements[i].onclick;
// Replace the input element with the new button element inputElements[i].parentNode.replaceChild(buttonElement, inputElements[i]); } } }); </script>
How to switch the submit button from an input element to a button element
SOLVE
Yep, that was the idea 🙂
I also wanted to give anyone else the chance to provide a simpler solution if I missed it e.g. there's a hidden setting in Hubspot that does this already, lol.
May 21, 20247:38 AM - edited May 21, 20247:40 AM
Community Manager
How to switch the submit button from an input element to a button element
SOLVE
Hi @DaveMyb, thank you so much for sharing the solution with the Community! ❤️ Much appreciated!
This will be really helpful to other Community Members, that's great!
Sorry, I thought you were looking for a workaround 😀. Thanks @Jnix284 for the clarification!
Have a lovely day!
Best, Bérangère
HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates. Learn More.
HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates. Learn More.
How to switch the submit button from an input element to a button element
SOLVE
That is a useful post too, thanks! I was not really looking for a particular answer here, just sharing what I figured out in case someone else had the same issue. We're a bit limited with what we can easily do in terms of styling an <input type="submit">.
How to switch the submit button from an input element to a button element
SOLVE
Yep, that was the idea 🙂
I also wanted to give anyone else the chance to provide a simpler solution if I missed it e.g. there's a hidden setting in Hubspot that does this already, lol.