views:

33

answers:

2
function replaceIfEmpty(fieldID, value){
    alert($j('input#'+fieldID.val()));
    if ($j('input#'+fieldID).val() == ""){
        $j('input#'+fieldID).val(value);
    }
}

there's my function, and this is in my controller:

      page << "replaceIfEmpty('object_name', '#{t.name}');"

but when all this is invoked, an alert tells me:

RJS error:

TypeError: Object object_name has no method 'val'

even though I'm using jQuery 1.3.2, the docs say .val() isn't new to 1.4 =\

A: 

Based on your error, it looks to me as though fieldID is not what you think it is. Have you debugged using Firebug at all?

JasCav
+1  A: 

Your parenthesis are a bit off, this:

alert($j('input#'+fieldID.val()));

Should be:

alert($j('input#'+fieldID).val());

Currently you're trying to call .val() on the string fieldID, rather than the jQuery object.


A bit of a tangent from the issue here: If you upgrade to 1.4+ you can make this a bit shorter by passing a function to .val(), like this:

function replaceIfEmpty(fieldID, value){
  $j('#'+fieldID).val(function(i, oldVal) {
    alert(oldVal);
    return oldVal == "" ? value : oldValue;
  });
}
Nick Craver
thanks for the 1.4 tip. Over teh next couple weeks I'm upgrading all the javascript, I'll keep this in mind.
DerNalia