Reputation: 204
I'm familiar with using ID's to help display your JavaScript string text onto your HTML webpage like so:
<html>
<body>
<button onClick = "display()">Press Me</button>
<p id = "text"></p>
<script>
function display() {
var string = "hello";
var message = document.getElementById("text");
message.innerHTML = string;
}
</script>
</body>
</html>
But what if I wanted to display "hello" multiple times by the press of the button? I don't want to keep adding id's the same way I did "text" to appear. Is there anyway I could simply print out the text once you press the button for however many times you'd like in vanilla JavaScript?
Also, I am aware of "repeat()
" but that's not what I'm looking for.
Upvotes: 1
Views: 1194
Reputation: 42054
But what if I wanted to display "hello" multiple times by the press of the button?
It's enough to change this line:
message.innerHTML = string;
to:
message.innerHTML += string; // add at the end....
<button onClick = "display()">Press Me</button>
<p id = "text"></p>
<script>
function display() {
var string = "hello ";
var message = document.getElementById("text");
message.innerHTML += string;
}
</script>
Upvotes: 3