tags:

views:

37

answers:

1

how to alert if input type file is empty !?

this is my jquery code but i didnt know how to get the value

$('.upload').live("click",function()
{
    var imgVal = $('#uploadImage').val();
    if(imgVal=='')
    {
        alert("empty input file");

    }
    return false;

});


<input type="file" name="image" id="uploadImage" size="30" />
<input type="submit" name="upload"  class="send_upload" value="upload" />
+2  A: 
<script type="text/javascript">
$(document).ready(function() {
    $('#upload').bind("click",function() 
    { 
        var imgVal = $('#uploadImage').val(); 
        if(imgVal=='') 
        { 
            alert("empty input file"); 

        } 
        return false; 

    }); 
});
</script> 

<input type="file" name="image" id="uploadImage" size="30" /> 
<input type="submit" name="upload" id="upload"  class="send_upload" value="upload" /> 
Fosco
nah , this wont work . but thanks for your try
Mac Taylor
This works... http://fosco.com/test2.php
Fosco
added form tag to test. it still works.
Fosco
pay close attention, i added an ID tag to the upload button and changed your selector and function used to assign the event handler.
Fosco
that was a smart answer , u proved u r a real coder :D
Mac Taylor
@Fosco is right the problem is that `$(".upload")` won't bind to button as it has `send_upload` applied to it. `upload` is its ID.
TheVillageIdiot