0

I'm creating a custom form and am trying to add a button to the form, and have functionality when its clicked.

I don't really have much experience with javascript, so if there is a better approach to this I'm open to suggestions.

So what I have is

<asp:Button runat="server" Text="test" id="Group1" onclientclick="updateText()" />
<script type="text/javascript">
    function updateText() {
             document.getElementById('<%=Group1.ClientID%>').value="updated";
    }
</script>

I've tried both putting this in the form (changing the < and > to < and >) as well as putting it in a content editor webpart (with the asp:Button section remaining on the form, and the script being placed in a .js file). The function calls properly in both instances, but I can't get the getElementById to work-trying an alert with the value also fails.

1 Answer 1

1

Try this

<asp:Button UseSubmitBehavior="false"  runat="server" Text="test" 
 id="Group1" onclientclick="updateText(); return false;" />

<script type="text/javascript"> 
    function updateText() {             
             document.getElementById('<%=Group1.ClientID%>').value="updated";
             return false; 
    } 
</script>

Returning false will remove the postback.

<asp:controlname /> type directives won't work in a content editor. In asp or a content editor you can do this with a regular html button instead of an asp control if you want to.

<input type="button" id="btn" value="test" onclick="updateText2();"/>

<script type="text/javascript"> 
    function updateText2() {            
             document.getElementById('btn').value="updated";             
    } 
</script>
3
  • Thanks for the reply, that was a question I had. I am receiving the error: Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E) Timestamp: Wed, 29 Aug 2012 20:33:38 UTC Message: Object expected Line: 881 Char: 1 Code: 0 URI: <form url> Commented Aug 29, 2012 at 20:35
  • Is there more to the error? Object expected on line 881 is too vague. What code is on that line? Commented Aug 29, 2012 at 20:48
  • Your second solution worked, thanks! The failing line was the getItemById call Commented Aug 29, 2012 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.