John English
John English

Reputation: 121

Retaining button visible when onClick function activated

Hi I have something similar. when I click MESSAGE1 button the the text Hello World appears and the button disappears. When the button is clicked how do I retain the button alongside the text?

<button onclick="document.write('<?php echo 'Hello World'; ?>');">MESSAGE 1</button>

Upvotes: 0

Views: 171

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388416

Instead of using document.write, you can create a html element place holder and then set its content

<button onclick="document.getElementById('message').innerHTML = '<?php echo 'Hello World'; ?>';">MESSAGE 1</button>
<span id="message"></span>

The button is remove because, from this doc

document.write is recklessly dependent on timing. If document.write is called before the onload event, it appends or inserts text into the page. If it is called after onload, it completely replaces the page, destroying what came before.

Upvotes: 1

Related Questions