Support

Account

Home Forums Backend Issues (wp-admin) ACF Javascript API dynamic elements not saved

Unread

ACF Javascript API dynamic elements not saved

  • Hello!
    I want to dynamically add “options” to a “select” field using Javascript.
    I achieved it, but after saving the page, the selected option is lost and not saved in the database.
    This is my code:

    
    acf.add_action('load_field/key=${key}', () => {
        getElements().then((element) => {        
            const selector = document.querySelector('[data-key=${key}]').querySelector('select');
            for (let i = 0; i < element.length; i++) {
                let opt = document.createElement('option');
                opt.value = element[i].id;
                opt.innerHTML = element[i].description;
                selector.appendChild(opt);
            }
        });
    })
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.