Question

Something in my code isn't working and I'm not sure what it is. I want this function to return false, and at the same time append some text to a DIV

$(document).ready(function(){
$('#newCatchForm').submit(function() {
    if ( !$('#t1')[0].value && !$('#t2')[0].value ) { 
        return false;
        $('#divTest').append("this text was appended");
 }
});
});
Was it helpful?

Solution

return halts execution. Nothing else will get executed after return...

switch the two around:

$(document).ready(function(){
  $('#newCatchForm').submit(function() {
    if ( !$('#t1')[0].value && !$('#t2')[0].value ) {
      $('#divTest').append("this text was appended");
      return false;
    }
  });
});

OTHER TIPS

The return statement doesn't only determine the return value, it also exits from the function, so you should put that last:

$(document).ready(function(){
  $('#newCatchForm').submit(function() {
    if ( !$('#t1')[0].value && !$('#t2')[0].value ) { 
      $('#divTest').append("this text was appended");
      return false;
    }
  });
});

return false will stop the execution of the function, therefore the next instruction will never be executed. Try this:

$(document).ready(function(){
    $('#newCatchForm').submit(function() {
        if ( !$('#t1')[0].value && !$('#t2')[0].value ) { 
            $('#divTest').append("this text was appended");
            return false;
        }
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top