18

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

1
  • 5
    type TextBox don't exists ... use instead <input type="text" id="color" /> link Commented Apr 11, 2011 at 17:22

4 Answers 4

39

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

$("#color").prop('disabled', true);

2 Comments

jQuery is not JavaScript. please do not answer JavaScript questions with jQuery responses.
actually it is. It's written entirely in javascript.
2

Here was my solution:

Markup:

<div id="name" disabled="disabled">

Javascript:

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

This the best solution for my applications - hope this helps!

Comments

0

You can use disabled attribute to disable the textbox.

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

1 Comment

The accepted answer has already provided the solution you recommend 5+ years ago!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.