dianping/cat · error · Error
cannot call val() if initSelection() is not defined
Error message
cannot call val() if initSelection() is not defined
What it means
Error "cannot call val() if initSelection() is not defined" thrown in dianping/cat.
Source
Thrown at cat-home/src/main/webapp/assets/js/uncompressed/select2.js:2493
this.select
.val(val)
.find("option").filter(function() { return this.selected }).each2(function (i, elm) {
data = self.optionToData(elm);
return false;
});
this.updateSelection(data);
this.setPlaceholder();
if (triggerChange) {
this.triggerChange({added: data, removed:oldData});
}
} else {
// val is an id. !val is true for [undefined,null,'',0] - 0 is legal
if (!val && val !== 0) {
this.clear(triggerChange);
return;
}
if (this.opts.initSelection === undefined) {
throw new Error("cannot call val() if initSelection() is not defined");
}
this.opts.element.val(val);
this.opts.initSelection(this.opts.element, function(data){
self.opts.element.val(!data ? "" : self.id(data));
self.updateSelection(data);
self.setPlaceholder();
if (triggerChange) {
self.triggerChange({added: data, removed:oldData});
}
});
}
},
// single
clearSearch: function () {
this.search.val("");
this.focusser.val("");
},View on GitHub (pinned to e815e74d4c)
Solutions
- Define the initSelection option when attaching Select2 to a hidden input so val() can resolve ids to data.
- Set data via select2('data', ...) instead of relying on val().
When it happens
Trigger: Triggered when val() is called on a Select2 attached to an <input type='hidden'> without defining initSelection() to map ids to objects.
Common situations: See trigger scenarios.
AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14).
Data as JSON: /api/errors/89f0b7f3d55f516f.
Report an issue: GitHub.