javascript not working

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?
 
G

Guest

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 %>');
 
T

tshad

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
 
G

Guest

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%>
 
G

Göran Andersson

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top