how to enqueue javascript to manipulate acf input field in admin?

Question

I have an acf field ‘select’ that i want to remove some options from using js

but i can not acces the field it returns undefined

<select id="acf-block_5f35006ff2da4-field_5f3cce8a89941" 
class="" 
name="acf-block_5f35006ff2da4[field_5f3cce8a89941]" 
data-ui="0" data-ajax="0" 
data-multiple="0" 
data-placeholder="Select" 
data-allow_null="0">
<option value="transparent">Transparent/bakgrundsfärg</option>
</select>

I have tried with

    document.addEventListener("DOMContentLoaded", function(event) {
     
     console.log("loaded") //Prints out: loaded


      var element = document.getElementById("acf-block_5f35006ff2da4-field_5f3cce8a89941");
      console.log(element)// Prints out: undefined


}),

It seams that the block is not finished loading when DOMContentLoaded is triggerd

add_action('admin_enqueue_scripts', function ($hook) {



    $file = sprintf("%s/js/wp-admin_blocks.js", get_template_directory());

    if(file_exists($file)) {

        $mtime = filemtime($file);
        wp_enqueue_script('ams-blocks', sprintf("%s/js/wp-admin_blocks.js", get_template_directory_uri()), [], $mtime, true);
    
    }


});
0
Stefan Avramovic 3 years 2020-08-20T15:10:36-05:00 0 Answers 39 views 0

Leave an answer

Browse
Browse