1

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..

4
  • Are you sure that testBoxTemp exists? sounds like you're using ASP controls - view-source the page and double check the element's id Commented May 17, 2012 at 17:52
  • yes its _ctl21_m_txtTemp Commented May 17, 2012 at 17:57
  • can you show the aspx page markup related to txtTemp? Commented May 17, 2012 at 18:00
  • <td style="height: 19px"> <input name="_ctl21:m_m_txtTemp" type="text" id="_ctl21_m_m_txtTemp" class="inputNormalRequired" style="width: 32px;" size="1" value="100" /> </td> Commented May 17, 2012 at 18:04

6 Answers 6

2

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;
Sign up to request clarification or add additional context in comments.

6 Comments

tried all these ideas.. it does not seem to work.. I am suspecting the problem lies somewhere else.. if the field is in a frame inside the page, how do I get hold of the id and disable it in that case..because right now, no matter what I try, jqyer, javascript and so many of these possible solutions..it does not disable the control at all..
Just so I am understanding, you have a page that has an iframe, and you are trying to disable a textbox within the iframe from the parent page.?
I dont know that.. its in a frame.. or not.. this should be simple to fix and no matter what I try its not working.. makes me think that the control is not straightforward on the page.. may be its not finding the id if I do it this way..
I think we need to see more of your html to really be able to help you.
|
1

You tagged Jquery, so I suppose I can provide a jquery solution:

if (some_condition)
{
  $('#_ctl21_m_txtTemp').attr('disabled','disabled');
}

1 Comment

I tried this.. but does not work for me.. a alert before the jquery line works showing me its getting into the if statement. but the jquery line does not work..
0

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.

Comments

0

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');

1 Comment

it's the generated name since it's a server control
0

according to http://www.w3schools.com/tags/att_input_disabled.asp

the syntax should be

textBoxTemp.disabled="disabled"  

you could give that a try.

Comments

0

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);

1 Comment

tried both.. it does not seem to work.. I am suspecting the problem lies somewhere else.. if the field is in a frame inside the page, how do I get hold of the id and disable it in that case..because right now, no matter what I try, jqyer, javascript and so many of these possible solutions..it does not disable the control at all..

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.