Discussions

How do you use javascript to auto-populate a hidden form field?

Michael JC Zack
Michael JC Zack Posts: 4 Green Ribbon

Hello all

I have been working on honeypots to protect a few forms we have. For the purposes of this question, I am using a field we created called "botEquationValue".


This code is not auto-populating the form field. What would be the correct way to approach this issue?

Answers

  • Cyrielle
    Cyrielle Posts: 8 Red Ribbon

    botEquationValue is probably the HTML name, not the id, so you should probably be for the name with getElementsByName

  • Michael JC Zack
    Michael JC Zack Posts: 4 Green Ribbon

    Thanks for the suggestion. I updated it:


    Unfortunately, it is not working. I don't think my initial approach was working, which is very unfortunate. Does anyone have experience with how to approach this?

  • Cyrielle
    Cyrielle Posts: 8 Red Ribbon
    edited May 17, 2022 5:55PM

    It should be working ; add a listener on page load to make sure the form has loaded before you call for the field

    window.addEventListener("load", function() {

     document.getElementById("fe7642").value="999";  

    });

    However, if you use the id, you will have to change it for each form you create. I would suggest using the HTML name instead so you can templatize your Landing Pages

    window.addEventListener("load", function() {

     document.getElementsByName("honeypot")[0].value="999";  

    });

    Hope this helps