Sandor Sandor - 1 month ago 11
Javascript Question

jQuery getting text content of a span

I am trying to write a delete function in Dropzone.js. In order to do that I need the id of the file the way it was uploaded.

I tried to get a property of an object with no success. Now I am trying to use jQuery to get the value or text content of the span that has it.

enter image description here

this is the screenshot of the structure. The jQuery code I am trying is:

var loooot = $(".dz-filename").parents('span').text();


To be more specific I am trying to get the number 1_1477778745352 (which is a time stamp).

The Dropzone code is as follows:

<script>

var listing_id = "1";

// these are the setting for the image upload
Dropzone.options.pud = {
acceptedFiles: ".jpeg,.jpg,.png,.gif",
uploadMultiple: false,
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 1, // MB
addRemoveLinks: true,
maxFiles: 10,
renameFilename: function (filename) {return listing_id + '_' + new Date().getTime();},
init: function()
{
this.on("removedfile", function(file)
{
var loooot = $("span", ".dz-filename").html();
alert(loooot);
});
}
};
</script>

Answer

It becomes undefined because dropzone works dynamicly, use this:

$('body').find(".dz-filename").find('span').text();

Best way to do this is to declare dropzone:

//first declare somewhere variable
var my_drop;

// then on creating dropzone:
my_drop = new Dropzone('.dropzone', { 
/* your setup of dropzone */ 
});

Then you can retreive information about files with this:

my_drop.files[0].name

The [0] represent's first file, you can loop through them if there's more then one.