Reputation: 5027
I am trying to disable a control on a page using javascript.. I am doing the following but not able to do it successfully.. anything I am missing, please point out..
if (some condition) {
var textBoxTemp = document.getElementById('_ctl21_m_txtTemp');
textBoxTemp.disabled = true;
}
I got the element id by looking at the source of the page. In the source the id shows
_ctl21:m_txtTemp
I tried using both
_ctl21:m_txtTemp and _ctl21_m_txtTemp
in the second line of code above..
Upvotes: 1
Views: 624
Reputation: 28635
You can use jquery prop() to disable the textbox.
$("[id$=txtTemp]").prop("disabled", true);
Note: prop() is only available after jquery 1.6. If you are using an earlier version, you can use attr() instead:
$("[id$=txtTemp]").attr("disabled", "disabled");
Edit:
Since ASP.NET tends to mangle the ID of a control, there are several different ways you can find the control on the page such as:
$('[id$=txtTemp]')
$('#<%= txtTemp.ClientID %>')
Edit:
Since you seem to think that your control could be in an iframe, I found this solution from the following stackoverflow question: how-can-i-access-iframe-elements-with-javascript
function iframeRef( frameRef ) {
return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument
}
var iframe = iframeRef( document.getElementById('iframeID') )
You will now be able to search the iframe content as if it was the document via:
iframe.getElementByID('<%= txtTemp.ClientID %>').disabled = true;
Upvotes: 2
Reputation: 1654
I can't tell if you're wanting this in jQuery or plain old Javascript, but here it is in both:
Javascript
(you're probably looking for the readonly attribute instead of disabled)
var textBoxTemp = document.getElementById('_ctl21_m_txtTemp');
textBoxTemp.readonly = true;
jQuery
$('#_ctl21_m_txtTemp').attr('readonly',true);
Upvotes: 0
Reputation: 521
according to http://www.w3schools.com/tags/att_input_disabled.asp
the syntax should be
textBoxTemp.disabled="disabled"
you could give that a try.
Upvotes: 0
Reputation: 504
With jQuery, I wouldn't use document.getElementById, that totally not the point
textBoxTemp = $('#_ctl21_m_txtTemp'); // weird name, by the way
textBoxTemp.attr('disabled','disabled');
Upvotes: 0
Reputation: 2311
if you're using jQuery you can do
$('#<%= txtTemp.ClientID %>').attr("disabled", "disabled");
This ensures you get the right client id in case you move the textbox around.
Upvotes: 0
Reputation: 6045
You tagged Jquery, so I suppose I can provide a jquery solution:
if (some_condition)
{
$('#_ctl21_m_txtTemp').attr('disabled','disabled');
}
Upvotes: 1