Search code examples
javascriptwordpressformsninja-forms

Set Ninja form input value via JS


My issue is as following: I am trying to fill a ninja form field via JS, as I am filling my form on several sections of the page. So section A fills the name for example..

I am using such code

document.querySelector("input[value=setJob]").value = text

Such code is setting the value in the input, but when I send the form, the default value ("setJob") is set in the submission results. Also tried to focus on the input before setting its value, same result

Appreciate any help

Thanks in adavnce


Solution

  • It seems Ninja forms has a documentation for this case: https://developer.ninjaforms.com/codex/changing-field-values/

    as it says:

    jQuery( '#nf-field-' + fieldID ).val( newValue ).trigger( 'change' );
    

    of course, change the jquery select method

    It seems when updating value, you should trigger change event