3

I call this function on a form element, and based on what the json object contains, change the background of a form element. Essentially a simplified validation - but I don't wanna use jQuery's bloated validation plugins.

IV.validSimple ( { obj: '#email', event: 'blur', check: 'emailexists' } ); 

The problem is (the closure context/scope drives me mad :), how can I pass the d variable (object) to the success callback (_IV.bool) in the code below.

var IV = { urlBase: '/oink/ajax/', validSimple: function(d) { var _IV = this; $(d.obj).bind(d.event, function() { $.ajax ({ url: _IV.urlBase + d.check + '?' + $(d.obj).val(), async: true, dataType: 'json', success: _IV.bool, }); } ); }, bool: function(data) { if (data.ok == 1) $(obj).css('backgroundColor','#c5e8c5'); else { $(obj).css('backgroundColor','#f7c7c7').focus(); } } //function }; 

2 Answers 2

4

Alternatively, you could set the context option:

$.ajax({ url: _IV.urlBase + d.check + '?' + $(d.obj).val(), dataType: 'json', context: d.obj, // or context: d , don't know which one you want success: _IV.bool, }); 

and use $(this) inside bool instead of $(obj).

Use whatever way seems more logical to you.

Sign up to request clarification or add additional context in comments.

Comments

3

just do

... success: function(data) { _IV.bool(data, d) } ... 

in your query initialization code and redefine bool this way:

... bool: function(data, d) { ... 

1 Comment

Ah darn, it's too early in the morning here :DD. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.