Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fernando 11 posts 79 karma points
    Jun 15, 2022 @ 13:55
    Fernando
    0

    Adding javascript script to submit button in a Form

    Hi, I have an Umbraco form and I want to add a js script in order to be able to validate some html inputs that are dynamically created in js side. How can I do this? For now, I will try to add a function in the onclick event in "submitbtn" button, but not sure if this is the standard way or if there is a better way to do this. Thanks!

  • Clydejohnson 2 posts 72 karma points
    Jul 05, 2022 @ 08:58
    Clydejohnson
    0

    Did you get any solution?

  • Fernando 11 posts 79 karma points
    Jul 05, 2022 @ 11:29
    Fernando
    0

    Yes, I added a function in the onclick event in "submitbtn" button, but not sure if this is the standard way or if there is a better way to do this. Are you using forms in Umbraco 9? If that is the case, I made this:

    //* Adds a listener to onclick event in order to be able to validate
    //* data and prevent to submit the form in case data is missing.
    const addSubmitOnClick = () => {
    
        var formName = document.getElementsByName("FormName")[0].value;
        if (formName == "Opportunity Page")
            document.getElementsByName("submitbtn")[0].onclick = validateOpportunityRegistration;}
    

    I have a field type that uses a js, then added that field type to the form and the code above to that js. You can return false in the function used to validate data (validateOpportunityRegistration in my case).

    Note: addSubmitOnClick(); is called when the js script is loaded.

Please Sign in or register to post replies

Write your reply to:

Draft