views:

858

answers:

5

I am counting words in a text field and after a certain amount of words, I use prevent default. In the else, I would like to renable the default commands.

Does preventDefault() have an opposite function?

Here is some sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>preventDefault</title>
 <style type="text/css"></style>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
  <script type="text/javascript">
      $(function() {
          var wordNum = 3;
          var input_length;
          $("#max").text("max word(s): " + wordNum);
          $("#test").keypress(function(event) {

              input_length = $.trim($(this).val())
       .replace(/\s+/g, " ")
       .split(' ').length;

              if (input_length > (wordNum - 1)) {
                  event.preventDefault();
              } else {
                  return true;
              }
          });
      });

  </script>
  </head>
  <body>
      <div id="max"></div>
   <textarea id="test" cols="20" rows="5"></textarea>
</body>
</html>

It seems to work on IE, but Firefox doesn't like it.

+3  A: 

Shouldn't

if maxwords
   preventDefault
else
   return true

do the trick ?

Les
You don't even need to return true.
David Dorward
I thought so too, but I'm using jQuery's keypress method. So once the goal is reached, there is no way to "undo" it.
qwertypants
+1  A: 

simply use return true; at the end of your eventHandler. That would bubble up the event to the browser.

Tzury Bar Yochay
+1  A: 

Doesn't look like it, https://developer.mozilla.org/en/DOM/event , but you can just not call it...

Kobi
@Downvoter - My answer was written before code was posted, and the code suggests confusion at best. The opposite of preventDefault is the default - do nothing.
Kobi
+1  A: 

I think the problem you are having is that you are not looking for the delete key. Preventdefault does not cancel the event handler all together. But with your code once you hit the maximum length of your field the user will no longer be able delete any characters because the delete keypress is being cancelled.

The reason it works in IE is because IE does not fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab. In Safari the keycode for delete is incorrect in the keypress event.

For these reasons I have a twofold suggestion; first, use the keydown event instead so that you get the correct keycodes.

Second, look at the keycode and if it is delete or backspace then don't preventDefault.

if ((event.keyCode != 46 && event.keyCode != 8) || input_length > (wordNum - 1)) {
 return false;
} else {
 return true;
}
Pickle
Nice explanation, I think you've found the real issue here.
Kobi
This is exactly what I needed to happen. Thank you.
qwertypants
A: 
$("#delete_button").click(function(e){
            var category = $("#category").val();
            var answer = confirm("Are you sure you would like to delete the " + category + " category?");
            if (!answer)
                e.preventDefault();
        });