I have made a php form wherein I would like to display a loading gif if certain conditions are met. Here is a readable psuedo-code/example of the logic I need to employ in jquery.
if (file_input_is_set && submit_button_is_clicked) {
<div class="progress-bar">tada...loading_gif</div>
}
I have these two codes which work well individually. First code shows the loading gif when file input is set.
jQuery().on('change', 'input[type="file"]', function(e) {
Second code shows the loading gif when the form-submit-button is clicked.
jQuery("input[name='profile_submit']").click(function (e) {
Since I know almost nothing about jQuery, I need help to put these events together. Forgive me if my terminology is wrong. Here is what I have tried that do not work, it shows the loading gif even before I press the submit button. So I need a function which works like the php's &&
operator, and I dont know how you do that in jquery.
jQuery().on('change', 'input[type="file"]', function(e) {
jQuery("input[name='profile_submit']").click(function (e) {
//jQuery('#submit-button').hide();
jQuery('.progress-bar').fadeIn(1000);
});
});
Note: The reason you see a lot of jQuery
in the code is due to wordpress ( in which the form is used) as it needs it to be in no-conflict mode.
Rephrased Question ( with hint from @webkit) : How do I also check if file_input_is_set/valid when submit event triggered ?
Start with the last action, form submit. then check for existence of file in file input:
jQuery("input[name='profile_submit']").click(function (e) {
if( jQuery('input[type="file"]').val() != '' ){
jQuery('.progress-bar').fadeIn(1000);
}
});
JSFiddle demo: http://jsfiddle.net/nDNP5/
you can do this:
jQuery("input[name='profile_submit']").click(function (e) {
// check to see if file is valid
if (jQuery('input[type="file"]').val() != "") {
// load
jQuery('.progress-bar').fadeIn(1000);
}
});
Or this: (Only when file has been selected will you activate submit btn)
jQuery('input[type="file"]').on('change', function (e) {
// check to see if file is valid
if (jQuery('input[type="file"]').val() != "") {
jQuery("input[name='profile_submit']").off().on('click', function () {
jQuery('.progress-bar').fadeIn(1000);
});
} else {
jQuery("input[name='profile_submit']").off()
}
});