1

I have the following piece of code, which changes one line of text in a click of a button:

<!DOCTYPE html>
<html>
<body>

<h1>Change content exercise</h1>

<p id="demo">Watch this HTML content changes..</p>

<button type="button"
onclick="document.getElementById('demo').innerHTML = 'This is JavaScript!'">
Click Me!</button>

</body>
</html>

This is quite easy since there is no script, no function needed to handle the button. Now, I want this same button to change back to the first content when I click it again. I assume that now I need to have a function, but not sure how to write it. An ideas?

7 Answers 7

2

You don't have to use a function. You could do it with a ternary operator ? and :, or you could even just write an if else statement all on one line.

<h1>Change content exercise</h1>

<p id="demo">Watch this HTML content changes..</p>

<button type="button"
onclick="document.getElementById('demo').innerHTML === 'This is JavaScript!' ? document.getElementById('demo').innerHTML = 'Watch this HTML content changes..' : document.getElementById('demo').innerHTML = 'This is JavaScript!';">
Click Me!</button>

However, that is a lot of code to cram into one line and it would be much cleaner in a separate function, as such.

function changeText() {
  var demo = document.getElementById('demo');
  if (demo.innerHTML === 'This is JavaScript!') {
    demo.innerHTML = 'Watch this HTML content changes..';
  } else {
    demo.innerHTML = 'This is JavaScript!';
  }
}
<h1>Change content exercise</h1>

<p id="demo">Watch this HTML content changes..</p>

<button type="button" onclick="changeText()">Click Me!</button>

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

1 Comment

I would always use a function, it makes things clearer. Actually, I would not use the onclick attribute at all, to have JS and HTML separated nicely.
2

Well. Although the way you are trying is not the best practice.... But the following way will give you some hope. try to do more research.

function myFunction() {
    var x=document.getElementById("demo").innerHTML;
if(x=="A Paragraph."){
document.getElementById("demo").innerHTML="Back Again";}

if(x=="Back Again")
document.getElementById("demo").innerHTML="A Paragraph.";

}
   

<p id="demo">A Paragraph.</p>

<button type="button" onclick="myFunction()">Try it</button>

Comments

1

More simply, this function works:

<h1>Change content exercise</h1>

<p id="demo">Watch this HTML content changes..</p>

<button type="button" onclick="changeText()">Click Me!</button>

Javascript:

function changeText() {
    e = document.getElementById('demo');
    e.innerHTML = e.innerHTML == "Watch this HTML content changes.." ? "This is JavaScript!" : "Watch this HTML content changes..";
}

You can see it working at this JS Fiddle: http://jsfiddle.net/0yLb4a3j/

Comments

1

You can have something like a toggle function:

<script type="text/javascript">
    function toggleContent() {
        var message1 = "This is JavaScript!";
        var message2 = "Watch this HTML content changes..";
        var element = document.getElementById('demo');

        if (element.innerHTML===message1)
            element.innerHTML = message2;
        else
            element.innerHTML = message1;

        return false;
    }
</script>

You get it called by setting onclick="toggleContent();" on the button.

Comments

1

You could use an IIFE, an array, an incremented counter, and a modulo operator to achieve this.

document.getElementById('button').onclick = (function(){
    var demo = document.getElementById('demo');
    var text = [demo.textContent,'This is JavaScript!'];
    var count = 0;
    return function() {
        demo.textContent = text[++count % 2];
    }
})();
<p id="demo">Watch this HTML content changes..</p>
<button type="button" id="button">Click Me!</button>

Comments

0
var btn = document.getElementById("<btn_id>");
var previous = "";

btn.addEventListener("click", clickHandler);

function clickHandler() {
  var demo = document.getElementById("demo");
  if (!previous) {
    previous = demo.innerHTML;
  } else {
    demo.innerHTML = "This is JS";
    btn.removeEventListener("click", clickHandler);
  }
}

Comments

-1

first of all , you ll need to do the code in a seperate script, in brief , append the intial text to the div then wheck button clicked, change it to second text, and according to your question you ll need a second button who will change the div text to the intial text , logically that ll give something like tht :

<script> 
window.onload = function(){
document.getElementById('demo').innerHTML = 'Watch this HTML content changes..'
}
document.getElementById('button1').Onclick = function(){
document.getElementById('demo').innerHTML = 'This is JavaScript!'
};
document.getElementById('button1').Onclick = function(){
document.getElementById('demo').innerHTML = ''Watch this HTML content changes..'
};

</script>

3 Comments

OP wants to do it with only 1 button.
Also, typo at ''Watch this html content changes...' Quote isnt properly wrapped.
Oups ..my bad , didn't understand the question at first, gimme one minute

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.