3

I am facing a problem with a JavaScript readonly field. I have a textboxes which I am making readonly.

 if(val == true)
{    
    ddlCnt.value=objRec.iCntId;
    tAdd1.value=objRec.sAddress1;        
    tAdd2.value=objRec.sAddress2;        
    tCity.value=objRec.sCity;
    tState.value=objRec.sState;
    tZip.value=objRec.sZip;

    //tAdd1.disabled = tAdd2.disabled = tCity.disabled = tState.disabled = tZip.disabled = ddlCnt.disabled = true;
    //tAdd1.disabled = tAdd2.disabled = tCity.disabled = tState.disabled = tZip.disabled = ddlCnt.disabled = true;
    tAdd1.setAttribute("readonly", true);
    tAdd2.setAttribute("readonly", true);
    tCity.setAttribute("readonly", true);
    tState.setAttribute("readonly", true);
    tZip.setAttribute("readonly", true);
}

It is working fine. Now to deactivate this readonly property I used

else
{    
    tAdd1.value = tAdd2.value = tCity.value = tState.value = tZip.value = "";        
    //tAdd1.disabled = tAdd2.disabled = tCity.disabled = tState.disabled = tZip.disabled = ddlCnt.disabled = false;
    ddlCnt.value="-1";
    vsRec.innerHTML='';     
    tAdd1.setAttribute("readonly", false);
    tAdd2.setAttribute("readonly", false);
    tCity.setAttribute("readonly", false);
    tState.setAttribute("readonly", false);
    tZip.setAttribute("readonly", false);

    //vsRec.style.visibility='hidden';             
}

But it is not working at all. Can any one please help me out of this problem or any suggestion or tips that can help me a lot regarding this (and why this is not working?).

4 Answers 4

7

You need to remove the attribute with removeAttribute.

tAdd1.removeAttribute("readonly");
tAdd2.removeAttribute("readonly");
tCity.removeAttribute("readonly");
tState.removeAttribute("readonly");
tZip.removeAttribute("readonly");
Sign up to request clarification or add additional context in comments.

1 Comment

I will must accept your answer. It is not giving the permission to accept your answer before 3 minutes.
3

You need to remove the readonly attribute, e.g.:

tAdd1.removeAttribute("readonly");

Comments

3

readonly is not a true/false attribute, it's a present/not-present attribute. You need to remove the attribute, not set it to false.

Comments

3

The presence of the readonly attribute causes the input box to become read-only, not setting it to true or false. Remove the attributes.

Comments

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.