tony
tony

Reputation: 2392

Typescript, javascript, better way of writing this

This will be an easy question for someone, typescript doesn't like the below syntax, how do I rewrite it using jQuery 'on'?

    var funcA= "FuncA('" +id+ "'," +max+ ");";
    $textbox[0].onkeyup = new Function(funcA));

I could possibly pass id and max in a closure but I'd like to know how to use 'Function' with the jQuery 'on' function

It would also be useful to know if it's possible to specify a region in typescript as ignorable, to be able to say just treat this block as valid javascript

Thanks

Upvotes: 0

Views: 122

Answers (3)

basarat
basarat

Reputation: 276181

Just use the .on function. It should compile fine:

$textbox.on('keyup',()=> FuncA(id,max));

Upvotes: 0

Razem
Razem

Reputation: 1441

How did you ever come up with this? Every time you realize you are writing a code to a string, you know you are doing it wrong. The eval function and the Function constructor should be forgotten.

$textbox[0].onkeyup = function () { FuncA(id, max); }; // Solution 1
$textbox[0].onkeyup = FuncA.bind(null, id, max); // Solution 2

// Using jQuery
$($textbox[0]).keyup(FuncA.bind(null, id, max));

Upvotes: 4

Youness
Youness

Reputation: 1495

try this :

    $textbox[0].keyup(function(){FuncA(id,max);});

Upvotes: 0

Related Questions