Salesforce Steps
Salesforce Steps

Reputation: 183

Make TextArea Disabled in JS

I want to make Textarea Disable (Grayed out) in my JS Method

function myfun(status){

if(status=='Yes'){
    $('input[id$="txtareaID"]').attr('disabled','disabled'); 
}

The above code is not working for this.

Upvotes: 0

Views: 1579

Answers (6)

Salesforce Steps
Salesforce Steps

Reputation: 183

none of the below answers worked. Then I found something amazing trick which solved my problem --- Here it is --- JUST REMOVE "input" word from that line in if block -

WORKED CODE :

function myfun(status){

if(status=='Yes'){
    $('[id$="txtareaID"]').attr('disabled','disabled'); 
}

Previous CODE :

 function myfun(status){

    if(status=='Yes'){
        $('input[id$="txtareaID"]').attr('disabled','disabled'); 
        $('input[id$="txtareaID"]').prop('disabled',true); //Didn't worked either
    }

Upvotes: 0

Srihari
Srihari

Reputation: 766

<textarea> is what you should be looking for not the <input> tag with id = textareaid.

$('input[id$="txtareaID"]').attr('disabled','disabled');

change the above code to the below one and see the magic by clicking the link at the bottom.

$('textarea[id$="txtareaID"]').attr('disabled','disabled');

http://jsfiddle.net/5s9ge7d6/1/

Upvotes: 0

Israel Fonseca
Israel Fonseca

Reputation: 1083

Your problem is with CSS not JS. As far I can tell your code is working, but there's no way of changing this specific style. You could try workarounds: Change Font Color For Disabled Input

Upvotes: 0

Beri
Beri

Reputation: 11600

If your selector is correct, than you need only to change attr to prop:

function myfun(status){
   if(status === 'Yes'){ // more suitable for comparing
       $('input[id$="txtareaID"]').prop('disabled',true); 
   }
}

Related post: Disable/enable an input with jQuery?

Upvotes: 2

joews
joews

Reputation: 30330

You should use prop instead of attr:

$('input[id$="txtareaID"]').prop('disabled', true); 

jQuery docs

Upvotes: 2

Rakesh_Kumar
Rakesh_Kumar

Reputation: 1442

Considering textarea as

<textarea id='txt'>Something</textarea>

Using jQuery, you can achieve like this

$("#txt").attr('disabled', true);

Using plain javascript

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

Upvotes: 0

Related Questions