javascript not working

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have a control that I need to run some javascript on.

This used to work in 1.1 and I am now on 3.5 and it doesn't seem to work.

If I do:

**********************************************
<script type="text/javascript">
function CheckSave() {
var field1 = document.getElementById('txtSINInput');

if (field1.value == "") <--------Error
prompt("field1 is blank");
if (field1 == null)
prompt("field1 is null");

if (!confirm('You have made change(s).\nDo you want to leave
without saving?'))
return true;
}

</script>
*********************************************

The javascript gets called fine, but I get an error that says

"Microsoft Jscript runtime error: Object Required".

The object txtSINInput is there:

<asp:TextBox ID="txtSINInput" style="float:left;
margin-right:5px;" runat="server"
CssClass="TextBox" Width="80px" MaxLength="11"
/>

This is on the same page.

Why doesn't it find it?
 
I have a control that I need to run some javascript on.

This used to work in 1.1 and I am now on 3.5 and it doesn't seem to work.

If I do:

**********************************************
<script type="text/javascript">
    function CheckSave() {
        var field1 = document.getElementById('txtSINInput');

        if (field1.value == "")              <--------Error
            prompt("field1 is blank");
        if (field1 == null)
            prompt("field1 is null");

            if (!confirm('You have made change(s).\nDo you want to leave
without saving?'))
        return true;
    }

</script>
*********************************************

The javascript gets called fine, but I get an error that says

"Microsoft Jscript runtime error:  Object Required".

The object txtSINInput is there:

                          <asp:TextBox ID="txtSINInput" style="float:left;
margin-right:5px;"  runat="server"
                             CssClass="TextBox" Width="80px" MaxLength="11"
/>

This is on the same page.

Why doesn't it find it?

Because you cannot use ASP.NET control id in the client script. If you
look at view source code in the browser, you will see that there is no
control with id="txtSINInput". ASP.NET generates its own id.

Use ClientId property to make it working.

Example:

var field1 = document.getElementById('<%= txtSINInput.ClientId %>');
 
Because you cannot use ASP.NET control id in the client script. If you
look at view source code in the browser, you will see that there is no
control with id="txtSINInput". ASP.NET generates its own id.

Use ClientId property to make it working.

Example:

var field1 = document.getElementById('<%= txtSINInput.ClientId %>');

I did find out how to do (similar to your way) by using:

function SetBirthDate(sender) {
var field1 = $get('<%=txtBirthDateInput.ClientID%>');

field1.value = sender.getSelectedDateFormatted();

return true;
}

Not sure why my other code didn't work.

Here is my code from one of my old pages from ASP.Net 1.1:

*******************************************************
<script language=javascript>
function CheckSave()
{
var field1 = document.getElementById('CompanyName');
var field2 = document.getElementById('Position');

if ((field1.value != "") || (field2.value != ""))
if(!confirm('You have made change(s).\nDo you want to leave without
saving?'))
return false;
return true;
}
</script>
*******************************************************

Here I didn't have to worry about how .net changed the ID's.

I assume that they changed how it worked in 3.5.

Also, not sure why $get works. I just found that in another piece of code.

Thanks,

Tom
 
I did find out how to do (similar to your way) by using:

    function SetBirthDate(sender) {
        var field1 = $get('<%=txtBirthDateInput.ClientID%>');

        field1.value = sender.getSelectedDateFormatted();

        return true;
    }

Not sure why my other code didn't work.

Here is my code from one of my old pages from ASP.Net 1.1:

*******************************************************
<script language=javascript>
function CheckSave()
{
  var field1 = document.getElementById('CompanyName');
  var field2 = document.getElementById('Position');

 if ((field1.value != "") || (field2.value != ""))
  if(!confirm('You have made change(s).\nDo you want to leave without
saving?'))
   return false;
 return true;}

</script>
*******************************************************

Here I didn't have to worry about how .net changed the ID's.

I assume that they changed how it worked in 3.5.

Also, not sure why $get works.  I just found that in another piece of code.

Thanks,

Tom

$get works because of ASP.NET "injection"

<%=txtBirthDateInput.ClientID%>
 
tshad said:
Here I didn't have to worry about how .net changed the ID's.

I assume that they changed how it worked in 3.5.

No, it hasn't changed. It depends on where you have the controls. The id
is only changed if the control is inside a naming container (e.g. a user
control).
 
Back
Top