简体   繁体   中英

How to add 2 onclick functions

<!DOCTYPE html>
<html>
<body>

<h1>A452 Question 2 - Arrays</h1>

<p>Array of Products:</p>
<p id="prod"></p>
<p id="prod2"></p>
<p id="prod3"></p>
<p id="text1"></p>
<p id="text2"></p>

<script>
var products = ["Printer","Tablet","Router","Keyboard","Monitor","Mouse"];
document.getElementById("prod").innerHTML = products
</script>

<button type = "button" onclick = "document.getElementById('prod2').innerHTML = products.sort();">
Turn list in alphabetical order!</button>

<button type = "button" onclick = "document.getElementById('prod3').innerHTML = products.length;">
Count the number of items in the list!</button>

</body>
</html>

How do I add a string of text before each onclick event. For example the output for the second button is "6" while i want it to be "the number of items in the list is: 6"

您可以只连接字符串:

<button type = "button" onclick = "document.getElementById('prod3').innerHTML = 'the number of items in the list is: ' + products.length;">Count the number of items in the list!</button>

Hello put the text between 'text' and after add + .
like this:

<button type = "button" onclick = "document.getElementById('prod3').innerHTML = 'the number of items in the list is: ' + products.length;">

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM