I have this:
var field = query("form#"+form.id+" input[name='" + error.field + "']");
if(field.length){
if( field[0].id ) {
widget = registry.byId( field[0].id );
if(widget){
...
}
}
I would have thought I could write:
var field = query("form#"+form.id+" input[name='" + error.field + "']");
if(field.length && field[0].id && widget = registry.byId( field[0].id) ){
...
}
But if I use the second, “shortened” form, I get a Javascript error.
Sorry, it would be messy to give a JSFiddle. The problem happens when query() returns 0 values — field[0].id && widget = registry.byId( field[0].id) were still interpreted.
I thought that I was playing it safe as the second field[0].id would only happen if field.length was > 0 and then widget = registry.byId( field[0].id) would only happen if field[0].id is true…
What am I missing?
Merc.
You may be getting
Uncaught ReferenceError: Invalid left-hand side in assignmenterror which is because you are trying to evaluateregistry.byId()method on RIGHT and assigning it intoresulton LEFT but as you’ve not restricted your left-hand assignment using brackets, the entirefield.length && field[0].id && widgetis considered which raised an error.You can suppress the error by wrapping your last condition in brackets.
if(field.length && field[0].id && (widget = registry.byId( field[0].id)) ){