I’ve edited your code a little and the alert()
runs for each selected item. Note that in your code you execute a return
too early that make the code bellow don’t run after the first run. Also, you use the same name for a function and for a string var, which is not very appropiate.
function get_the_extension(url) {
var ext=(url = url.substr(1 + url.lastIndexOf("https://wordpress.stackexchange.com/")).split('?')[0]).substr(url.lastIndexOf("."));
return ext;
}
jQuery(document).ready(function($){
var custom_uploader;
$('#songbook_addfile_button').click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title:"blemblem",
button: {
text:"blemblem"
},
multiple: true
});
custom_uploader.on('select', function() {
var selection = custom_uploader.state().get('selection');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
var the_extension = get_the_extension(attachment.url).replace('.','');
alert(the_extension);
//...one commented line, that was to add files into HTML structure - works perfect, but only once
});
});
custom_uploader.open();
});
});