views:

58

answers:

6

Hi,

Can someone tell me why the code below doesn't work. I think my theory is sound, I am just missing some vital component.

'#tweet' is the id assigned to the form submit button. I want it to check whether the input has less than 141 characters, if it doesn't, alert the user and do not submit the form.

Currently it does nothing.

    $('#tweet').click(function() {
   if ($('.message').val().length > 140) {
    alert('Your message much be less than or exactly 140 characters');
    return false;
   }
  });
A: 

The ID should be "tweet" not "#tweet"

Dested
#tweet is correct for selecting an element by id with jquery
David Archer
You got 4 downvotes I figured someone should tell you that $('#tweet') is absolutely correct -- why did you think it wasn't?
Erik
He distinctively said "'#tweet' is the id assigned to the form submit button". He should absolutely not have <input id="#tweet" ... />
Dested
Nope. In jQuery you use the # to select by ID. (.for classes or noting to select elements) $("") is not equivalent to document.getElementByID("")
James Wiseman
In @Dested's defence, the OP does say the id is "#tweet", and that isn't a valid id for an HTML element. However, it also seems likely that the OP means that the id is "tweet" and is using "#tweet" as the selector string. So, @Dested's answer is technically correct but doesn't really answer the question.
Tim Down
that's why I said he should paste the HTML part too because it can be that he named the ID as `#tweet` at the `<form>` instead of `tweet`.if he did this way it @Dested has the right answer.
KARASZI István
+1  A: 

you should attach your behaviour to the form submit event:

 $('form').bind('submit',function() {
   if ($('.message').val().length > 140) {
    alert('Your message much be less than or exactly 140 characters');
    return false;
   }
  });
pixeline
+2  A: 

Put the above code like this

<script type="text/javascript">
$(function(){
  $('#tweet').click(function() {
    if ($('.message').val().length > 140) {
     alert('Your message much be less than or exactly 140 characters');
     return false;
    }
  });
});
</script>

in the section of your html after including jQuery library file. As event handlers are attached after the dom is ready.

GeekTantra
A: 

you probably want to prevent the default action instead of returning false

$('#tweet').click(function( e ) {
    if ($('.message').val().length > 140) {
        alert('Your message much be less than or exactly 140 characters');
        e.preventDefault();
    }
});
meouw
+1  A: 

it may be possible, that your .message hits more elements!

$(document).ready(function() {
    $('#tweet').click(function(e) {
        var success = true;
        $('.message').each(function() {
            if ($(this).val().length > 140) {
                alert('Your message much be less than or exactly 140 characters');
                success = false;
            }
        });
        if (!succeess) {
            e.preventDefault();
        }
        return success;
    });
});
Andreas Niedermair
A: 

For completeness, if your preventing default, you should probably go the following extra distance:

function stopEvent(event) {
    event.preventDefault();
    event.stopPropagation();
    if ($.browser.msie) {
        event.originalEvent.keyCode = 0;
        event.originalEvent.cancelBubble = true;
        event.originalEvent.returnValue = false;
    }
}

There's mroe info on this in my answer to the following question: http://stackoverflow.com/questions/2118560/jquery-form-submission-stopping-page-refresh-only-works-in-newest-browsers

James Wiseman