I am saving a text file as index.html and using that to test this function, however nothing happens when I click the button.
<!DOCTYPE html>
<html>
<body>
<button onclick="testamber()">Click here</button>
<script>
var testamber = function() {
var ambersands = ["&", "&&", "&&&", "&&", "&",]
var text = "";
var i;
for (i = 0; i < ambersands.length; i++) {
text += ambersands[i] + "<br>";
}
};
</script>
</body>
</html>
I need to render a pattern of ampersands to the browser like this & && &&& && &
You can replace the browser html using document.write
:
<body>
<button onclick="testamber()">Click here</button>
<script>
function testamber() {
var ampersands = ["&", "&&", "&&&", "&&", "&" ]
var text = "";
for (var i = 0; i < ampersands.length; i++) {
text += ampersands[i] + "<br/>";
}
document.write(text);
};
</script>
</body>
or you can provide somewhere for it to go, eg <div id='output'></div>
and set that, leaving the button, eg $("#output").text(text);
. This uses jquery, but you can find equivalent in standard js.
You need to output the results .
var testamber = function(){ var ambersands = ["&", "&&", "&&&", "&&", "&",] var text = ""; var i; for (i = 0; i < ambersands.length; i++) { text += ambersands[i] + "<br>"; } document.getElementById("output").innerHTML = text; };
<button onclick="testamber()">Click here</button> <div id="output"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.