+ 1
Can you help me with this?
https://code.sololearn.com/Wq34fJ3duUmx/?ref=app I want the number to change after the button is clicked but it goes to nan
16 Respuestas
+ 4
function add() {
let name = document.getElementById("input");
let namelabel = document.getElementById("student1");
namelabel.textContent = name.value;
}
function paws() {
let paws = document.getElementById("paws");
let pawslabel = document.getElementById("counter");
pawslabel.textContent = paws.value + ' Paws';
}
function take() {
let paws = document.getElementById("paws");
let pawslabel = document.getElementById("counter");
let count = +paws.value;
count = count<=0 ? 0 : count - 1;
paws.value = count+'';
pawslabel.textContent = count + ' votes';
}
+ 3
Thx Bob_Li
And Azmi Deliaslan
+ 3
Bob_Li
It looks better than mine and i wont copy it. Thanks for the code in js though near the start of this disscution!
+ 2
function take() {
var x = document.getElementById("paws").value;
x = x - 1;
document.getElementById("counter").innerHTML = x + " votes";
}
when u try this, it works for one time
+ 2
Hello it works great but can it possibly make it so it can happen as many times as i want, Thanks!
+ 2
Bob_Li It is because i didnt save it when i tried it because of the error
+ 2
True well still i wont copy it.
+ 1
Blocky Badboy suggestion to change the input type, specially for the paws input to "number" so input errors can be avoided.
<input id="input" type="text" placeholder="Student">
<button onclick="add()">Add student</button>
<br><br>
<input id="paws" type="number" placeholder="Paws Of Student">
<button onclick="paws()"> Add paws</button>
+ 1
Bob_Li Hi i tried it and i found an error and it said couldnt find a varuble paw at line 25
+ 1
"
Bob_Li Hi i tried it and i found an error and it said couldnt find a varuble paw at line 25
"
?? your code seems to be working ok... ??🤔
+ 1
Blocky Badboy
I edited the code I posted.
modification to prevent negative count:
count = count<=0 ? 0 : count - 1;
+ 1
Hello Bob_Li
I tried it but it does not work because count is a varuble in a function and those varubles dont apply anywhere else but that function
+ 1
Blocky Badboy
count variable is supposed to be scoped to the function. Otherwise both counts would be the same.
Here is your code with the modifications
https://code.sololearn.com/WP1c5YvsIHYc/?ref=app
+ 1
it's your code, I just tweaked it.
+ 1
Blocky Badboy
I played around a bit more with your idea...
Building the counter entirely with javascript is a bit more work, but afterwards you can create as many counters as you want very easily.
https://code.sololearn.com/WKp6upWXUSwp/?ref=app
+ 1
Amazing