Q:

Ruby on Rails: jQuery why is val not defined?

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:

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;
  });
}

