Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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>
share|improve this answer
    
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> –  hrezs Aug 29 '12 at 20:35
    
Is there more to the error? Object expected on line 881 is too vague. What code is on that line? –  spnydtf Aug 29 '12 at 20:48
    
Your second solution worked, thanks! The failing line was the getItemById call –  hrezs Aug 29 '12 at 20:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.