Search code examples
javascriptundefined

Input Field returns undefined but works after changing the value


When I first click on this function. It returns an undefined value. When I click on the function again, this code works fine in this form and every form after that.

alert(characters[x].ac+"/"+this.value+"/"+this.ac) returns undefined/undefined/0 initially. It returns 26/undefined/0 when I click the function again and captures the value just the way I want. So it is basically saying that characters[x].ac is undefined when I initially click the bolded text to make the text field appear.

How do make the 'undefined' listing go away. It happens when I change the value of an array for the first time but works fine in the same array in every form after that.

function askAc(x) {
    if(this.ac!=0) {
        response=this.ac;
        characters[x].setAc(response);
        this.ac=0;
    } else {
        response="<input class=widgetstyle onClick=_setAc(this.value) size=2 type=text value="+characters[x].ac+">";
        characters[x].setAc(response);
    }
}
function _setAc(x) {
    this.ac=x;
    this.refresh();
}

Solution

  • Does your input field have a value="" attribute or is it missing? a missing value attr may result in undefined (maybe just in some browsers).

    It is not clear from your code which input field you are accessing.