admin管理员组

文章数量:1400115

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?

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?

Share Improve this question asked Dec 15, 2015 at 17:05 GuyTalGuyTal 2076 silver badges17 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 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>

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>

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/0yLb4a3j/

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.

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>

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);
  }
}

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>

本文标签: javascriptHow to change HTML content multiple times with one button using JS (no JQuery)Stack Overflow