senzacionale
senzacionale

Reputation: 20936

jQuery attr command

I want to add a value to the onchange event, but the JS code has errors

$(args).attr('onChange', 'setTimeout('__doPostBack(\'ctl00$cpBody$txtDate\',\'\')', 0)');

anyone see error?

Upvotes: 1

Views: 243

Answers (3)

Matt
Matt

Reputation: 75327

How about the proper jQuery/ best Javascript way:

$(args).bind('change', function () {
    setTimeout(function () {
        __doPostBack('ctl00$cpBody$txtDate','');
    }, 0);
});

Although why the 0ms timeout period?

You can change your code to:

$(args).bind('change', function () {
    __doPostBack('ctl00$cpBody$txtDate','');
});

With only the most minor functional difference (__doPostBack will be executed immediately instead of ASAP).

It's better to provide a function as the first argument of setTimeout, as opposed to a string. setTimeout is closely related to eval, and eval is bad.

Upvotes: 2

jAndy
jAndy

Reputation: 236162

$(args).bind('change', function(e){
    setTimeout(function(){
       __doPostBack('ctl00$cpBody$txtDate','');
    }, 0);
});

should fit a little bit better.

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176956

Try this :

$(args).bind('Change','setTimeout('
           __doPostBack(\'ctl00$cpBody$txtDate\',\'\')', 0)');

Upvotes: 1

Related Questions