1

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

3 Answers 3

1

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;
   } 
}
Sign up to request clarification or add additional context in comments.

7 Comments

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.
Thanks for your help, ah sorry i used ..val()==0, i tried undefined, cause when i put alert(..val()) it returns undefined.
I think it would work with if (!val()). Also, I changed around my answer a bit to make more sense, hope that helps.
Thanks again, but it still dies on new elements added by AJAX.
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?"
|
0
function getXMLHTTP() {
    var xmlhttp=false;  
    try{
    xmlhttp=new XMLHttpRequest();
    }
    catch(e){       
    try{            
    xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e){
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e1){
    xmlhttp=false;
      }
     }
  }
   return xmlhttp;
}

function getModel(idCar) {      
    var strURL="http://localhost/root509dir/Includes/model.php?idCar="+idCar;
    var req = getXMLHTTP();
    if (req) {
          req.onreadystatechange = function() {
          if (req.readyState == 4) {
          if (req.status == 200) {document.getElementById('modelCont').innerHTML=req.responseText;
     } else {
       alert("There was a problem while using XMLHTTP:\n" + req.statusText);
       }
    }               
}           
    req.open("GET", strURL, true);
    req.send(null);
    }       
}

model.php

if(isset($_GET['idCar'])){
    $idCar = mysql_real_escape_string($_GET['idCar']);
}
$modelQR = mysql_query("SELECT *FROM model WHERE from_make = '$idCar'") or die(mysql_error());?>
<?php echo htmlentities(PUB_AN_MOD); ?><em>*</em>
<select name="carmodel" id="carmodel">
<option value="0">----------------</option>
<?php while($model = mysql_fetch_array($modelQR)){?>
<option value="<?php echo htmlentities($model['id_model']);?>">
          <?php echo htmlentities($model['car_model']);?></option>
<?php }?>
</select>

cars.php

<?php
$QRcars = mysql_query("SELECT *FROM car ORDER BY car_make") or die(mysql_error());
if(mysql_num_rows($QRcars)>0){?>
<select name="cars" id="cars" onchange="getModel(this.value);"><option value="0">-------------------</option>
<?php while($cars = mysql_fetch_array($QRcars)){?>
<option value="<?php echo htmlentities($cars['id_car']);?>">
<?php echo htmlentities($cars['car_make']);?></option>
<?php }?>
</select>       
<?php }?>
<span id="modelCont">
<?php echo htmlentities(PUB_AN_MOD); ?><em>*</em>
<select name="model" id="model">
<option value="0">---------</option>
</select>
</span>

Comments

0

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

Comments

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.