2

I'm trying to disable textbox using jquery:

$('#<%=txtFrom.ClientID %>').attr('disabled', true);

It looks as disabled: image, but actually it's not, I can write there. Why?

UPDATE: Fixed by adding input in the code:

$('#<%=txtFrom.ClientID %> input').attr('disabled', true);
Alexan
  • 8,165
  • 14
  • 74
  • 101
  • 1
    If what you want is just read only: http://stackoverflow.com/questions/1306708/add-readonly-to-input-jquery – Mario Corchero Feb 14 '13 at 21:30
  • 4
    Seems to [**work**](http://jsfiddle.net/vzGHm/) for me, but `prop()` is actually the preferred method, and did you remember document.ready ? – adeneo Feb 14 '13 at 21:31
  • Of course, I used after document.ready, actually after Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded. – Alexan Feb 14 '13 at 21:33
  • Mario, you mean I should use ReadOnly instead disabled? Disabled worked for me before. – Alexan Feb 14 '13 at 21:34
  • added input, it looks it's working: $('#<%=txtFrom.ClientID %> input').attr('disabled', true); – Alexan Feb 14 '13 at 21:37
  • 1
    Sounds like your jQuery selector was wrong, punch `$('#<%=txtFrom.ClientID %>)` into the browser console debug window and see what you get. – Snixtor Feb 14 '13 at 21:39
  • Now it's working fine after I added input. So I think problem is resolved, although I don't know exactly what input means and why I need it. Anyway thank you for help. – Alexan Feb 14 '13 at 21:49
  • Snixtor, maybe you're right, txtFrom is id from ASP.NET, so txtFrom.ClientID should give client id. But anyway, it's working now and I just leave it as it is. – Alexan Feb 14 '13 at 21:57

1 Answers1

1

You need to do that:

$('#YOUR_ELEMENT_ID').attr("disabled", "disabled");
red_alert
  • 1,738
  • 14
  • 24
  • 1
    Why? If it works with true and false? I prefer to use boolean value, because actually I use some condition to enable and disable it. – Alexan Feb 15 '13 at 15:21