0

In the code below, I want to show the size of uploaded file in a div or label element. However, it doesn't work when I want to show it in an input element.

 <script>
    var maxfilesize = 2097152;//2MB;
    $('#fuAttachments').live("change", function () {
        $("#msg").text("");

        var tt = $(this).val();

        var size = this.files[0].size;
        $("#msg").append(Math.ceil(size / 1024));

    });
</script>
<input id="fuAttachments" type="file" />

I want to display the size of uploaded file in #msg

<input id="msg" type="text" />
1
  • Side note: Try and use .on(). As of jQuery 1.7, the .live() method is deprecated. Commented Jun 2, 2013 at 14:03

5 Answers 5

1

It should work when you replace this line:

$("#msg").append(Math.ceil(size / 1024));

With this:

$("#msg").val(Math.ceil(size / 1024));

The .val() function sets the value property of all matched tags, while the .append() function inserts content at the end of the element.

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

2 Comments

now i have another problem with this, if i use jquery.min.js then its working but if i use jquery.1.9.js then its not working, but i want to use jquery.1.9.js.
Why would you want to use jquery.1.9.js? The minified version saved bandwidth and reduces loading times. What is not working when you use the non minified version?
1

Use "val" instead of "append"

$('#msg').val(size);

Comments

0

when i want to show it as input value. use .val() ..append() will not work for input .

Comments

0

Try:

$("#msg").val(Math.ceil(size / 1024));

or

$("#msg").val(size);


Why .val?

Form jQuery Doc:
Get the current value of the first element in the set of matched elements or set the value of every matched element. The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.

Comments

0

Please use the following script:

<script>
 var maxfilesize = 2097152;//2MB;
 $('#fuAttachments').live("change", function () {
    var size = $(this).files[0].size;
    $("#msg").val(Math.ceil(size / 1024));

 });
</script>

Comments

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.