0

I have an html button, like this:

<button onclick="func()" id="accountDetails" runat="server"</button>

I already spent a lot of time scratching my head to find out how to change the button's text by click on it, I put the following func() that execute the onclick event (html):

<script type="text/javascript">
    function func() {
        document.getElementById('accountDetails').textContent = 'server';
    }
</script>

but the result is that the text on the button is changed just for a second, (i mean just when i click the button) and after that the old text is again shown on the button.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
D.Rotnemer
  • 203
  • 1
  • 4
  • 9
  • 1
    The default `type` for a button is `submit`. When a submit button is pressed, the page will be refreshed. You need to prevent this from happening. See here: https://stackoverflow.com/questions/932653/how-to-prevent-buttons-from-submitting-forms – Turnip Jul 18 '18 at 16:18
  • Your javascript runs before the page is completed? – jmag Jul 18 '18 at 16:26

2 Answers2

1

You can follow this html and script.

use input instead of button.

 <input onclick="func()" id="accountDetails" type="button" value="click"></input>

instead of

<button onclick="func()" id="accountDetails" runat="server"</button>

Then the document.getElementById('accountDetails') need to set value instead of textContent

function func() {
    document.getElementById('accountDetails').value  = 'server';
}
<input onclick="func()" id="accountDetails" type="button" value="click"></input>
D-Shih
  • 44,943
  • 6
  • 31
  • 51
0
const clickBtnHandler = (e) => {
    e.target.innerText = "new button text"; 
};
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • This won't help at all. It is just another way to do the same error the OP did. Please, read carefuly question, but also, when answering old questions, all the existing answers and comments, to be sure to bring something relevant. The problem here was that button was a submit button, and page was reloaded. Your answer does nothing to address that, and would suffer the exact same problem. – chrslg Nov 29 '22 at 00:47