2

Please can you help me, i don´t find a solution. I have a function with arguments and want to pass this function to a jquery bind-event:

function countChars23 (chars) {
    var thi = $(this);
    var len = $(this).val();
    if (len.length >= chars) {
        len = len.substring(0, 22);
        thi.val(len);

    }
}

Calling the function don´t work:

$('#name').bind('keyup', countChars23(10));

This don´t work either:

$('#name').bind('keyup', function() {
    countChars23(10);
}

This don´t work either:

$('#name').keyup(function() {
    countChars23(10);
}

Many Thanks for your help!

0

1 Answer 1

10

You can use the data parameter to .bind:

$('#name').bind('keyup', {chars: 10}, countChars23);

and then in your function replace the declared parameter with event and put this in the first line:

var chars = event.data.chars;

EDIT the reason your second and third attempts (with the extra function wrapper) don't work is because they don't set this properly. You would have had to have called it like this:

$('#name').keyup(function() {
    countChars23.call(this, 10);
}
Sign up to request clarification or add additional context in comments.

3 Comments

your first solution worked! my second and third attempts (see above) don´t work, don´t know why! Thanks a lot!
@drog21 I just figured out why they didn't work - see update.
@kongaraju perhaps you have a different problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.