How to disable button after clicking it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

How to disable button after clicking it?

with anything? did i need any frameworks? how to do it?

22nd Aug 2018, 3:33 AM
Jingga Sona
Jingga Sona - avatar
21 Answers
+ 10
You can do <button onclick = "this.disabled = true; ">Click me!</button>, which will disable itself after you click it. Also, are you making a clicker game? It seems like it.
22nd Aug 2018, 5:47 AM
Rowsej
Rowsej - avatar
+ 6
You should write .disabled=true; in js๐Ÿ˜‰
22nd Aug 2018, 4:07 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 6
22nd Aug 2018, 4:15 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 5
Muhd Khairul Amirin Bin Yaacob hmm... so "if" didnt effect .disabled options... THANKS A LOT!๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘
22nd Aug 2018, 4:13 AM
Jingga Sona
Jingga Sona - avatar
+ 5
Thanks Rowsej !!! i will do that step one by one! you are a kind people!๐Ÿ‘๐Ÿ‘๐Ÿ‘
22nd Aug 2018, 8:40 AM
Jingga Sona
Jingga Sona - avatar
+ 4
Muhd Khairul Amirin Bin Yaacob hmm... i tried to do .disabled but it didnt work
22nd Aug 2018, 4:06 AM
Jingga Sona
Jingga Sona - avatar
+ 4
Muhd Khairul Amirin Bin Yaacob i do that alredy... hmmm maybe i have to show you the code: https://code.sololearn.com/WdKTjRnmuC5E/?ref=app when the count become 50, you can tap "buy" i want the button disabled after i tap "buy"
22nd Aug 2018, 4:09 AM
Jingga Sona
Jingga Sona - avatar
+ 4
Jiะฟะฑะฑะฐ Sะพnะฐ Emperor Bob Thanks! Because the this.disabled = true; is in the onclick event of the button, the this gets changed to the button itself. So then you can disable it easily โ˜บ๏ธ
22nd Aug 2018, 7:55 AM
Rowsej
Rowsej - avatar
+ 4
Jiะฟะฑะฑะฐ Sะพnะฐ Are you making a clicker game?
22nd Aug 2018, 7:56 AM
Rowsej
Rowsej - avatar
+ 4
Rowsej yes... i dont know how so i want to make it step by step
22nd Aug 2018, 8:16 AM
Jingga Sona
Jingga Sona - avatar
+ 4
Jingga Sona Here are some quick steps: 1. Make a basic button so that when you click it (using JS), a score increases. (The score needs to have a label as well.) Style it as well, and make sure in the JS it increases the score by a variable called power, not just score++. 2. Make an upgrade button that is disabled at the start. Each time you press the clicker, then check if you have got a high enough score, and if you have, the un-disable the button (clicker.disabled = false;), take away the amount from the score, and then change the power. 3. Add more upgrade buttons, and some helpers. When you buy a helper, ever3y second it will add 1 (or any amount) to your score. 4. Make everything look really nice. 5. Have fun clicking away!
22nd Aug 2018, 8:29 AM
Rowsej
Rowsej - avatar
+ 4
Luis Oscar There is no javascript course about styling object.
23rd Aug 2018, 12:06 PM
Jingga Sona
Jingga Sona - avatar
+ 2
Rowsej thank you so much i was wondering how to use this.blank in js
22nd Aug 2018, 6:35 AM
Emperor Bob
Emperor Bob - avatar
+ 2
Rowsej WOWOWOWOW i didnt know that!!!! Thanks a lot!๐Ÿ‘
22nd Aug 2018, 7:52 AM
Jingga Sona
Jingga Sona - avatar
+ 2
Note, you can pass 'this' to the function being called: <button id="btn" onclick="a(this);">Disable me</button> function a(element) { element.disabled = true; } Passing 'this' works in expected ways with other elements too, like <select>
22nd Aug 2018, 4:28 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Thanks Kirk Schafer! Now i learn so much way how to disable a button๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€
22nd Aug 2018, 8:04 PM
Jingga Sona
Jingga Sona - avatar
+ 2
you can do that in javascript. jingga sona ,you can take a javascript course in sololearn to master that.
23rd Aug 2018, 11:59 AM
0xc4da
+ 2
Thanks jingga sona .
23rd Aug 2018, 12:10 PM
0xc4da