custom_uploader
is actually an instance of wp.media()
and not an HTML element, so the variable does not have a onselect
property, but it does have a on()
method/function which you can use to listen to the select
event which is triggered after an attachment is selected.
Secondly, an HTML element does not have a innerHTML()
method, only a property of the same name that we can use to change the inner HTML of the element like so: <Element>.innerHTML = 'foo <b>bar bold</b> baz'
. Please check the MDN web docs for more details.
So to fix the issues, change this:
custom_uploader.onselect = function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
console.log(attachment);
var uploadLogoImage = document.getElementById("upload_image").innerHTML(attachment.url);
}
to:
custom_uploader.on( 'select', function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
console.log(attachment);
var uploadLogoImage = document.getElementById("upload_image").innerHTML = attachment.url;
} );
Also, if #upload_image
is actually an <input />
or textarea field, then use the value
property to change the input/textarea value, or you could instead use setAttribute()
.