Any one can help me, to make one button can work to display from one value to another value? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 4

Any one can help me, to make one button can work to display from one value to another value?

14th May 2020, 3:06 PM
Unknown Ambusher 🗡️
Unknown Ambusher 🗡️ - avatar
6 Antworten
+ 2
Thanks alot guy
14th May 2020, 5:12 PM
Unknown Ambusher 🗡️
Unknown Ambusher 🗡️ - avatar
+ 1
Basically, you create a function; inside this function, you take the id of the text element you want to change, and you use document.getElementById(id).innerText to change the value. For example: function change() { document.getElementById("text").innerText = "Button clicked."; } This function is called on button onclick, which is a attribute. Your button ends up looking like this: <button onclick="run()">Click Me</button>
14th May 2020, 3:18 PM
Jianmin Chen
Jianmin Chen - avatar
+ 1
Jianmin Chen but how if i have five value, each clicking button will change the 1st value to next, until 5th value
14th May 2020, 3:33 PM
Unknown Ambusher 🗡️
Unknown Ambusher 🗡️ - avatar
+ 1
Can you explain that in more detail?
14th May 2020, 4:06 PM
Jianmin Chen
Jianmin Chen - avatar
+ 1
<btn>→(1st click) = value1; do this →(2nd click) = value2; do this, value1 removed →(3rd click) = value3; do this value1,value2 removed, →(4th click) = value4; do this, value1,2,3 removed →(5th click) = value5; do this, value1,2,3,4 removed
14th May 2020, 4:20 PM
Unknown Ambusher 🗡️
Unknown Ambusher 🗡️ - avatar
+ 1
Okay, first you'd probably need a array of the text values: var values = ["1", "2", "3", "4", "5"]; You would then make a variable to determine how much times the user has clicked: var clicked = 0; The main part if changing the onclick function to something like this: function run() { clicked += 1; document.getElementById("text").innerText = values[clicked]; } If the user clicks more then five times, this will return a error. To prevent this, just go back to the beginning of the array: if (clicked > 5) { clicked = 0; } This goes back to the beginning of the array. Place it after "click += 1;".
14th May 2020, 4:31 PM
Jianmin Chen
Jianmin Chen - avatar