4

I want to make a format like the following:

Phrase
[Button]

When the button is clicked, the 'phrase' changes and the button remains. I am able to make it so text appears, however I can not make it so the button stays. Does anyone have an idea of how this may be done? Thanks.

0

4 Answers 4

8

Final script

Javascript (replacement)

function displayPhrase()
{
    document.getElementById("demo").innerHTML = 'New Phrase';
}

HTML (Old phrase)

<span id="demo">Old Phrase</span>

HTML (The button)

<button type="button" onclick="displayPhrase()"></button>

Credit to above answers ^_^

Sign up to request clarification or add additional context in comments.

Comments

3

Take a look at this.

1 Comment

Thank you, I knew I saw how to do it somewhere and it was in my first javascript tutorial!
1

I am able to make it so text appears, however I can not make it so the button stays.

My guess is you are updating the element that also contained the button element, and this update is clearing the button.

HTML

<span id="phrase"></span>
<button id="change-phrase" type="button">Change Phrase</button>

JavaScript

var button = document.getElementById('change-phrase'),
    content = document.getElementById('phrase');

button.onclick = function() {
    content.innerHTML = 'Your new phrase';
};

jsFiddle.

Comments

1

Use this code:

HTML:

<h1> Welcome to the </h2><span id="future-clicked"></span>

<button onclick="clicked_on()">Click for future</button>

JS:

<script>
    function clicked_on(){
        document.getElementById('future-clicked').innerHTML = 'Future World';
    }
</script>

1 Comment

An explanation to your code would improve the quality of your answer.

Your Answer

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