1

I am trying to call input attribute value from child function.

Code is Here:

     $("input.ups").fileinput({
         uploadUrl: "/imalatci/products/uploadimages",
         allowedFileExtensions: ["jpeg","jpg", "png", "gif"],
         maxImageWidth: 500,
         maxImageHeight: 400,
         maxFileCount: 5,
         resizeImage: true,
         language:"tr",
         uploadExtraData: function() {
             return {
                 product_id: $(this).attr('product_id')
             };
         }
     }).on('filepreupload', function() {
         $('#kv-success-box').html('');
     }).on('fileuploaded', function(event, data) {
         $('#kv-success-box').append(data.response.link);
         $('#kv-success-modal').modal('show');
     });

I want to get $("input.ups") product_id attribute in this function:

uploadExtraData: function() {
                 return {
                     product_id: $(this).attr('product_id')
                 };
             }

But it is getting undefined error?

How can we fix this ?

2
  • Inside the uploadExtraData the this reference will not be the element which raised the event. You should declare a variable with the data you require in the outer scope to use within that handler. You haven't shown a full example of your code, so I can't tell you exactly how to do this. Commented Oct 28, 2015 at 8:46
  • This is full jquery code really Commented Oct 28, 2015 at 8:51

1 Answer 1

1

The scope of this within the uploadExtraData function is not the input.ups element which the plugin is being instantiated on. To achieve what you require you would need to loop over them individually so you can get a reference to the element. Try this:

$("input.ups").each(function() {
    var $input = $(this);
    $input.fileinput({
        uploadUrl: "/imalatci/products/uploadimages",
        allowedFileExtensions: ["jpeg","jpg", "png", "gif"],
        maxImageWidth: 500,
        maxImageHeight: 400,
        maxFileCount: 5,
        resizeImage: true,
        language:"tr",
        uploadExtraData: function() {
            return { product_id: $input.attr('product_id') };
        } 
    }).on('filepreupload', function() {
        $('#kv-success-box').html('');
    }).on('fileuploaded', function(event, data) {
        $('#kv-success-box').append(data.response.link);
        $('#kv-success-modal').modal('show');
    });
});

I would also suggest using a data-* attribute instead of a non-standard attribute like product_id.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. You saved my day
No problem, glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.