Javascript error - Name not declared and others

Associate
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
apologies for ninja edit, did something stupid while attempting to fix it and didn't notice.

Basically I have a simple javascript function that I want to check the value of a textbox and if it differs from another textbox, to come up with a warning.

The error is:

SCRIPT5007: Unable to get value of the property 'value': object is null or undefined
ManageAgencyAgents.aspx, line 660 character 13 - this refers to the inpLoginID line but I figure its going to complain about all of my documentgetelementbyIds

The code is:
Code:
 function checkForUsernameChange() {
            if (document.getElementById('inpLoginID').value != 0) {
                var txtUsername = document.getElementById('txtUsername').value;
                var inpUsername = document.getElementById('inpUsername').value;
                if (inpUsername != txtUsername) {
                    var answer = confirm("Are you sure you wish to change this name, the user " + inpUsername + " will be deleted from the system");
                    if (answer) {
                        return;
                    }
                    else {
                        document.getElementById('txtUsername').value = inpUsername;

                    }

                }

            }

            return;
        }
 
Last edited:
Check inpLoginID has a value attribute set, if it doesn't then you will get en error when attempting to return a value for an unset attribute.

I would also suggest using !== instead of != unless your not worried about type casting and the other silly slip ups that JS can throw when matching vars.
 
Thanks Phunky, will tighten up those if statements

Got slightly further with this, no errors at least:

Basically it doesn't pull the value from the hidden input in order to display which user will get deleted and instead of setting the value of the visible textbox to the hidden value when the user clicks cancel, it wipes it completely

Code:
 function checkForUsernameChange() {
            if (document.getElementById("<%=inpLoginID.ClientID%>").value !== 0) {
                var txtUsername = document.getElementById("<%=txtUsername.ClientID%>").value;
                var inpUsername = document.getElementById("<%=inpUsername.ClientID%>").value;
                if (inpUsername !== txtUsername) {
                    var answer = confirm("Are you sure you wish to change this name, the user " + document.getElementById("<%=inpUsername.ClientID%>").value + " will be deleted from the system");
                    if (answer) {
                        return;
                    }
                    else {
                        document.getElementById("<%=txtUsername.ClientID%>").value = inpUsername;

                    }

                }

            }

            return;
        }
 
It seems the issue is how I am trying to assign a value to inpUsername. In the asp side of things I am doing the below, how should it be done?

Code:
<input type="hidden" id="inpUsername" runat="server" value="<%#DataBinder.Eval(Container.DataItem,"txtUsername")%>" />
 
resolved the issue, for those interested I was being a spastic and just set the value of the hidden input in the vb file
 
Back
Top Bottom