+ 4
Any one can help me, to make one button can work to display from one value to another value?
6 Answers
+ 2
Thanks alot guy
+ 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>
+ 1
Jianmin Chen but how if i have five value, each clicking button will change the 1st value to next, until 5th value
+ 1
Can you explain that in more detail?
+ 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
+ 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;".