开发者

Invalid ID error when running javascript function in visual studio 2010

开发者 https://www.devze.com 2023-02-21 12:03 出处:网络
I work in visual studio. I get a runtime error: id invalid when I click on a button a javascript function is called, that works with the element id.

I work in visual studio. I get a runtime error: id invalid when I click on a button a javascript function is called, that works with the element id. But I can see the element with exact id in view source of the page, where is the problem ? i use just html element and get same error.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>View HTML</title>
<script type="text/javascript" language="javascript">
 Hmove=-100;
 function moveObjRight(obj) 
{    
obj.style.left=Hmove;
Hmove+=2;
开发者_StackOverflow社区if(Hmove<100)
    window.setTimeout("moveObjRight(" +obj.id+ ");", 0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="btnViewHtml" runat="server" Text="View Html" onclick="moveObjRight(JS)"/>
<IMG SRC="starflower.gif" ALT="Starflower" id="JS">
    <br />
    <br />

</form>
</body>
</html>


Button has already got ID on client, which is not JS

asp:Button ID="btnViewHtml"

I think that is cause of your problems


You need OnClientClick, not OnClick:

<asp:Button 
    ID="btnViewHtml" 
    runat="server" Text="View Html" 
    OnClientClick="moveObjRight(this); return false;"
/>

Also note the argument passed to the of the moveObjRight function: this.


You are passing the id of the button and the referencing it as a object in the code...

Instead of that use this <asp:Button ID="btnViewHtml" runat="server" Text="View Html" onclick="moveObjRight(document.getElementById('JS'))"/>

and in the script use this

window.setTimeout("moveObjRight(" +obj+ ");", 0);

0

精彩评论

暂无评论...
验证码 换一张
取 消