[SOLVED] How can we disable a text field by clicking on a button and enable it using the similar button? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 27

[SOLVED] How can we disable a text field by clicking on a button and enable it using the similar button?

I want to enable the text field by clicking on a button and disable the same by again clicking on it. I tried but I could only enable a disabled text field or the vice versa. But I'm not able to do both the task with same function. Can anyone help ??? I have attached the tried code below. https://code.sololearn.com/Wh6rQ1eUG33n/?ref=app

14th Jan 2019, 11:52 AM
Nova
Nova - avatar
6 Answers
+ 13
Thank You PapaBT and Rishi Anand for your help. The both of the method worked. Thanks Again 😄
14th Jan 2019, 12:07 PM
Nova
Nova - avatar
+ 7
function onclick_disable() { var x = document.getElementById("Text"); x.disabled = !x.disabled; }
14th Jan 2019, 12:02 PM
PapaBT
PapaBT - avatar
+ 6
replace your script with the following function onclick_disable() { var x = document.getElementById("Text"); if(x.disabled === true) { x.disabled = false; } else { x.disabled=true; } }
14th Jan 2019, 12:01 PM
Rishi Anand
Rishi Anand - avatar
+ 3
HTML <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div> <input type="text" id="txtInput"/> </div> <div style="padding:20px"> <input type="button" value="Click me!" onclick="Onlick_button();"> </div> </body> </html> ============================================================================================================================== JS function Onlick_button() { var txtBox = document.getElementById("txtInput"); (txtBox.disabled)?(txtBox.disabled=false):(txtBox.disabled=true); }
15th Jan 2019, 6:11 AM
Manjunath L
Manjunath L - avatar
+ 2
Whether i use <p> tag or not the results are same in html body .than what the need of using tag<p> ?
15th Jan 2019, 4:55 AM
Rishi Chauhan
Rishi Chauhan - avatar
+ 2
if you want to format a text like indenting making bold, italic and so on. The <p> tag plays a important role. Or if needed to apply generic style, <p> tag plays role
15th Jan 2019, 6:15 AM
Manjunath L
Manjunath L - avatar