+ 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
6 Réponses
+ 13
Thank You PapaBT and Rishi Anand for your help. The both of the method worked.
Thanks Again 😄
+ 7
function onclick_disable()
{
var x = document.getElementById("Text");
x.disabled = !x.disabled;
}
+ 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;
}
}
+ 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);
}
+ 2
Whether i use <p> tag or not the results are same in html body .than what the need of using tag<p> ?
+ 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