Why 'return false'?


#1


I'd like to know, why do we need a "return false" at the end of the main function, do we want to prevent the form from being submitted? why should we do that?

Also can someone please help to check if my code is correct? Cheers.

var template = function(text) {
  return '<p><input type="checkbox"><i class="glyphicon glyphicon-star"></i><span>' + text + '</span><i class="glyphicon glyphicon-remove"></i></p>';
};

var main = function() {
  $('form').submit(function() {
    var text = $('#todo').val();
    var html = template(text);
    $('.list').append(html);
    $('#todo').val("");
    return false;  
  });
};

$(document).on(
  'click', '.glyphicon-star', function(click) {
  $(this).toggleClass('active');
});

$(document).on(
  'click', '.glyphicon-remove',function(click){
  $(this).parent().remove();
});

$(document).ready(main);


#2

Hi @maylzh,

return false prevents the form from being submitted and refreshing the page, which causes the entry to be lost :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.