function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nikhil Shrigod 6Nikhil Shrigod 6 

Javascript problem

I have a problem with the following code:
<apex:page >
    
    <apex:form >
        <apex:commandButton value="Click" onclick="document.getElementById('sp').innerHTML='Hello'" />
<span id="sp"></span>
    </apex:form>

</apex:page>

When I click on the button the value I  am setting in the span tag ('Hello' in span)  appears for 1 second and again disappears, I want the text 'Hello' to remain in span after clicking on the button. Can somebody please help me with this?
Best Answer chosen by Nikhil Shrigod 6
RD@SFRD@SF
Hi Nikhil,

You are almost there:

Try this:
<apex:page >
    
    <apex:form >
        <apex:commandButton value="Click" onclick="document.getElementById('sp').innerHTML='Hello'" reRender="sp"/>
<span id="sp"></span>
    </apex:form>

</apex:page>

Do you notice the rerender attribute set to span id?

Hope it helps
RD

All Answers

Supriya Adlinge 1Supriya Adlinge 1
Hi Nikhil,

Please check below link may be it will helpful to you.
https://salesforce.stackexchange.com/questions/55191/problem-using-javascript-with-commandbutton

Regards,
Supriya Gore
 
RD@SFRD@SF
Hi Nikhil,

You are almost there:

Try this:
<apex:page >
    
    <apex:form >
        <apex:commandButton value="Click" onclick="document.getElementById('sp').innerHTML='Hello'" reRender="sp"/>
<span id="sp"></span>
    </apex:form>

</apex:page>

Do you notice the rerender attribute set to span id?

Hope it helps
RD
This was selected as the best answer
Nikhil Shrigod 6Nikhil Shrigod 6
Thank you RD and Supriya
RD@SFRD@SF
Good to know it helped you, Nikhil. Would be great if you could close this question and mark the best answer :)