+ 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

31st Oct 2022, 9:51 PM
Blocky Badboy
Blocky Badboy - avatar
16 Antworten
+ 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'; }
1st Nov 2022, 1:25 AM
Bob_Li
Bob_Li - avatar
1st Nov 2022, 1:28 AM
Blocky Badboy
Blocky Badboy - avatar
+ 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!
1st Nov 2022, 6:41 PM
Blocky Badboy
Blocky Badboy - avatar
+ 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
31st Oct 2022, 11:17 PM
Azmi Deliaslan
Azmi Deliaslan - avatar
+ 2
Hello it works great but can it possibly make it so it can happen as many times as i want, Thanks!
1st Nov 2022, 12:57 AM
Blocky Badboy
Blocky Badboy - avatar
+ 2
Bob_Li It is because i didnt save it when i tried it because of the error
1st Nov 2022, 3:02 AM
Blocky Badboy
Blocky Badboy - avatar
+ 2
True well still i wont copy it.
1st Nov 2022, 7:31 PM
Blocky Badboy
Blocky Badboy - avatar
+ 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>
1st Nov 2022, 2:34 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li Hi i tried it and i found an error and it said couldnt find a varuble paw at line 25
1st Nov 2022, 2:37 AM
Blocky Badboy
Blocky Badboy - avatar
+ 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... ??🤔
1st Nov 2022, 3:02 AM
Bob_Li
Bob_Li - avatar
+ 1
Blocky Badboy I edited the code I posted. modification to prevent negative count: count = count<=0 ? 0 : count - 1;
1st Nov 2022, 4:30 AM
Bob_Li
Bob_Li - avatar
+ 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
1st Nov 2022, 5:59 PM
Blocky Badboy
Blocky Badboy - avatar
+ 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
1st Nov 2022, 6:35 PM
Bob_Li
Bob_Li - avatar
+ 1
it's your code, I just tweaked it.
1st Nov 2022, 7:29 PM
Bob_Li
Bob_Li - avatar
+ 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
2nd Nov 2022, 8:25 AM
Bob_Li
Bob_Li - avatar
+ 1
Amazing
2nd Nov 2022, 3:49 PM
Asim Farheen ⭐⭐🤺👿👿
Asim  Farheen ⭐⭐🤺👿👿 - avatar