Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable TextBox using JavaScript?

earlier I asked for help disabling a Button when a drop down menu item was selected. I was given some code that did the trick but now I need the same with a Textbox and for some odd reason its not working can you have a look for me...

HTML:

<form id="frmColor">
    <input type='TextBox' id='color' />
    <input type='submit' value='Change Color' id="colorSubmit"/>
 </form>

Javascript:

  tools.eraser = function () {
    var tool = this;
    this.started = false;
    var varPenColor = "White";
    context.strokeStyle = varPenColor;
    document.getElementById('frmColor').colorSubmit.disabled=true;
    document.getElementById('frmColor').color.disabled=true;

Any ideas why it wont disable the text box?

Thanks

like image 657
Mark Avatar asked Apr 11 '11 17:04

Mark


People also ask

How do I disable a textbox?

We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.

How do I GREY out a textbox in JavaScript?

Javascript: tools. eraser = function () { var tool = this; this. started = false; var varPenColor = "White"; context.

How do you turn off a function in JavaScript?

To stop the execution of a function in JavaScript, use the clearTimeout() method.


2 Answers

Form elements can be accessed via the form's DOM element by name, not by "id" value. Give your form elements names if you want to access them like that, or else access them directly by "id" value:

document.getElementById("color").disabled = true;

edit — oh also, as pointed out by others, it's just "text", not "TextBox", for the "type" attribute.

You might want to invest a little time in reading some front-end development tutorials.

like image 120
Pointy Avatar answered Sep 22 '22 14:09

Pointy


With the help of jquery it can be done as follows.

$("#color").prop('disabled', true);
like image 35
vijay as vj Avatar answered Sep 19 '22 14:09

vijay as vj