1

I have a web form for data entry. The page requires an ID to be passed via query string. In the code behind I check the ID has been supplied. If it hasn't the form (in a panel) is hidden and an error message is displayed.

In the html I have a JQuery script block that attaches an event (downloaded date picker).

    window.onload = function() {
        new JsDatePick({
            useMode: 2,
            target: "txtDateOfIncident",
            dateFormat: "%d %M %Y"
        });
    };

If the code behind hides the form the JQuery throws an error Object Required

So in the JQuery I am trying to test existance of the object As per this post...

    window.onload = function() {
        if ($('#txtDateOfIncident').length) {
            new JsDatePick({
                useMode: 2,
                target: "txtDateOfIncident",
                dateFormat: "%d %M %Y"
            });
        }
    };

Now I get the Object Required error on the $('#txtDateOfIncident').length code.

Any help to resolve this would be appreciated.

Community
  • 1
  • 1
Fred
  • 5,663
  • 4
  • 45
  • 74
  • is `txtDateOfIncident` id of the element ? if it is a server side control, the id in client could be different. – Flakes Apr 18 '13 at 10:13
  • Yes the id is `txtDateOfIncident`. however the point is I want to test existance so even if it was different I need the code to run without errors. What I am basically asking is "is there an element on the page with this id" – Fred Apr 18 '13 at 10:21
  • Wont this work ? `if (document.getElementById('txtDateOfIncident')){..}` – Flakes Apr 18 '13 at 10:38
  • Yes it does but shouldn't the JQuery work too? – Fred Apr 18 '13 at 11:05
  • I am not `equipped` to answer that. sorry. – Flakes Apr 18 '13 at 11:06
  • Well thanks for your help I will run with what works! Post as an answer and I will accept. – Fred Apr 18 '13 at 11:09
  • Is jQuery loaded? Is your script running on $(document).ready (http://api.jquery.com/ready/)? – Irvin Dominin Apr 18 '13 at 11:24
  • Ive tried `$(document).ready` and get the same error `Object Required`. If the form is not hidden it all works fine. – Fred Apr 18 '13 at 14:18

1 Answers1

0

I think it would be better if you set the client id mode of txtDateOfIncident to ClientIDMode="Static".

<asp:TextBox ID="txtDateOfIncident"  runat="server" ClientIDMode="Static" /> 
Mico
  • 117
  • 1
  • 12