0

Currently I'm doing something like this in markup

<input type="text" ONKEYPRESS="InputNumeric(event);" id="txtNumber" />

But I want to use the jQuery bind method instead for all the obvious reasons.

jQuery(function($)
{
    $("#txtNumber").bind("keyup", InputNumeric(event));
});

But when I try the above I get the below error

"event is not defined"

What should this syntax look like?

EDIT

The actual solution I got working is shown below.

$("#txtPriority").keypress(function (e) { InputInteger(e); });

3 Answers 3

4
jQuery(function($)
{
    $("#txtNumber").bind("keyup", function(event) {InputNumeric(event);});
});
Sign up to request clarification or add additional context in comments.

2 Comments

Actually it should be: $(document).ready(function () { $("#txtNumber").bind("keyup", function(event) {InputNumeric(event);}); });
$(function () { is shorthand for $(document).ready() jQuery(function($) { is the same thing in noConflict mode.
3

looks like InputNumeric is an existing function that takes an event as parameter, In that case this should also work

$("#txtNumber").bind("keyup",InputNumeric);

Comments

2

Arguments passed back via jquery callbacks are always implied, so simply writing the function name is enough.

$("#txtNumber").bind("keyup",InputNumeric);

function InputNumeric(event){
    $(event.target).dosomething(); // is the same as
    $(this).dosomething();
}

Example: http://www.sanchothefat.com/dev/sfhelp/jquery-args.html

1 Comment

This is also more helpful when you use for loops as functions should not be defined within loops.

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.