views:

152

answers:

1

Microsoft JScript runtime error: 'txtGivenName_OnFocus' is undefined

After adding what I thought was unrelated javascript code to a web page, I am suddenly getting errors that suggest that the browser cannot locate a javascript function that, to me, appears plain as day in design mode.

I'm thinking that this is a load sequence order problem of some sort. Originally, my script was at the bottom of the page. I did this with the intent of helping my site's SEO ranking.

When I moved the function to the top of the web page, the error went away. Now it is back.

I have a feeling someone is going to suggest a jQuery solution to execute some code only when the page is fully loaded. I'm I ignorant of jQuery. IfjQuery is given in the answer, please explain what I need to do (references, placement of script files) for VS 2010 RTM.

I am trying to set the focus to the first textbox on the webpage and preselect all of the text in the textbox

More info:

If I disable this Validator, the problem goes away:

<asp:CustomValidator ID="valSpecifyOccupation" runat="server" ErrorMessage="Required"
                                            ClientValidationFunction="txtSpecifyOccupation_ClientValidate" 
                                            Display="Dynamic" Enabled="False"></asp:CustomValidator>



function txtSpecifyOccupation_ClientValidate(source, args) {

    var optOccupationRetired = document.getElementById("<%=optOccupationRetired.ClientID %>");

    if (optOccupationRetired.checked) {
        args.IsValid = true;
    }
    else {
        var txtSpecifyOccupation = document.getElementById("<%=txtSpecifyOccupation.ClientID %>");
        args.IsValid = ValidatorTrim(txtSpecifyOccupation.value) != "";
    }

}
A: 

Yep, I would say most likely it's a loading order issue as well. And... I would totally recommend jquery...

Wherever you are calling your JavaScript function txtSpecifyOccupation_ClientValidate; I would assume you are possible dynamically writing a script block to the page on load or something...

if this is the case. I would add the following to your generated script block...

 $(function() {
...  call to function
    txtSpecifyOccupation_ClientValidate();
...
    });

jquery is very easy to learn. http://docs.jquery.com/Main_Page

Jason