52

The code is:

<input ID="fileUpload1" runat="server" type="file"

The following works fine:

<input onchange="javascript:alert('hola');" ID="fileUpload1"  runat="server" type="file"

I'd like to get this result using jQuery, but that doesn't work:

$('#fileUpload1').change(function (e) {
    alert("hola");
});

I am missing something? (Edit: Yes I missed include the *.js file.)

2
  • 1
    The id of the element is "fileUpload1" not "fileupload1". Are you also running your code after the element exists and you have included jQuery etc? Also check your browser console for javascript errors. Commented Aug 8, 2012 at 8:40
  • I searched for similar keywords but I'm looking for "no jQuery" version... Commented Oct 14, 2016 at 5:30

6 Answers 6

122

Demo : http://jsfiddle.net/NbGBj/

$("document").ready(function(){

    $("#upload").change(function() {
        alert('changed!');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

12

Or could be:

$('input[type=file]').change(function () {
    alert("hola");
});

To be specific: $('input[type=file]#fileUpload1').change(...

Comments

5

This jsfiddle works fine for me.

$(document).delegate(':file', 'change', function() {
    console.log(this);
});

Note: .delegate() is the fastest event-binding method for jQuery < 1.7: event-binding methods

Comments

4

It should work fine, are you wrapping the code in a $(document).ready() call? If not use that or use live i.e.

$('#fileupload1').live('change', function(){ 
    alert("hola");
});

Here is a jsFiddle of this working against jQuery 1.4.4

3 Comments

live is deprecated, one should use on() nowadays
He is using v1.4.4 which doesnt support on()
live is deprecated for all jQuery versions, use .delegate
2
 $('#fileupload').bind('change', function (e) { //dynamic property binding
alert('hello');// message you want to display
});

You can use this one also

Comments

2

Try to use this:

HTML:

<input ID="fileUpload1" runat="server" type="file">

JavaScript:

$("#fileUpload1").on('change',function() {
    alert('Works!!');
});

1 Comment

Sorry for I didn't see the problem properly...now check @Esailija

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.