views:

99

answers:

3

Hello i have this following function:

... var model = $("#carModel");
.... model.change(validModel);

function validModel(){
     if(model.val() == 0){
            model.addClass("errorJS");
            return false;
            }else{
            model.removeClass("errorJS");
            return true;
            }  
    }

I am getting carmodel id from a select box generated after an AJAX call. I can get its value in the Firebug console, but the function doest not execute. Even tough i use model.livequery(validModel);

// The errorJS class puts a red border arround a element, if the function returns false

+1  A: 

Among other problems, model means nothing inside the function context. Also, it makes no sense to call change(validModel) with this function, as it only returns a true/false value. You'll need to do something like:

model.change(function() {
    var truefalse = validModel(this);
    //do something with truefalse
});

and change the definition of validModel to

var validModel = function(elem){
   if($(elem).val() == undefined){
       $(elem).addClass("errorJS");
       return false;
   } else {
       $(elem).removeClass("errorJS");
        return true;
   } 
}
colinmarc
also, your use of if val() = undefined doesn't ring true to me, but I can't remember off the top of my head if that's actually incorrect or just uncommon.
colinmarc
Thanks for your help, ah sorry i used ..val()==0, i tried undefined, cause when i put alert(..val()) it returns undefined.
jartaud
I think it would work with if (!val()). Also, I changed around my answer a bit to make more sense, hope that helps.
colinmarc
Thanks again, but it still dies on new elements added by AJAX.
jartaud
without seeing the ajax calls in question, or more of your code, I can't really help you. What do you mean, it "dies on new elements added by AJAX?"
colinmarc
i mean by "die on new element" :), the function does not execute for the selectbox generated after the ajax call. I ll post the ajax
jartaud
I got it, the problem was with the ready event, i defined the function out of it and voila.
jartaud
A: 
jartaud
A: 

I got it, the problem was with the ready event, i defined the function out of it and voila.

jartaud