1

I'm try to disable (and remove) some attributes in jQuery, but it seems not working when value is changed and set to "disabled" some fields. This script runs when a value of drop-down box is changed.

Here code below:

$(".tipo_frete").change(function(){
    if($(this).val()  == "FOB" ){
        $(".id_transp").attr("disabled");
        $(".id_transp").val("");
        $(".nome_transp").attr("disabled");
        $(".nome_transp").val("");
        $(".valor_frete").attr("disabled");                     
        $(".valor_frete").val("");                      
    }else{
        $(".id_transp").removeAttr("disabled");
        $(".valor_frete").removeAttr("disabled");           
        $(".nome_transp").removeAttr("disabled");           
    }
});

Did I do something wrong here?

1
  • In light of the answers, I think this question is OK, so I'll vote to reopen. (It might close as having too much of a niche solution, though, we'll see). Commented Dec 16, 2014 at 22:03

4 Answers 4

3

use prop()

disable

$(".id_transp").prop("disabled",true);

enable

$(".id_transp").prop("disabled",false);

NOTE : $(".id_transp").attr("disabled"); you have to pass 2 arguments like $(".id_transp").attr("disabled",true)

simplify your code

$(".tipo_frete").change(function () {
    if ($(this).val() == "FOB") {
        $(".id_transp,.nome_transp,.valor_frete").val("").prop("disabled", true);
    } else {
        $(".id_transp,.nome_transp,.valor_frete").prop("disabled", false);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

In jQuery when you want to get attribute value, must be use .attr('key') or .prop('key') with one argument, But when you want to set a value to an attribute, you must set two arguments like as .attr('key', 'value') or .prop('key', 'value')

$(".id_transp").attr("disabled", "disabled");

//OR

$(".id_transp").prop("disabled", true);

Comments

2

Yes, you are missing another parameter in attr() property.
Try this:

$(".tipo_frete").change(function(){
    if($(this).val()  == "FOB" ){
        $(".id_transp").attr("disabled","disabled");
        $(".id_transp").val("");
        $(".nome_transp").attr("disabled","disabled");
        $(".nome_transp").val("");
        $(".valor_frete").attr("disabled","disabled");                      
        $(".valor_frete").val("");                      
    }else{
        $(".id_transp").removeAttr("disabled");
        $(".valor_frete").removeAttr("disabled");           
        $(".nome_transp").removeAttr("disabled");           
    }
});

But when you remove those attibutes, you keep only one parameter inside and it will be removed.

1 Comment

That was a complete one. It tried to pass two arguments in parameter on removeAttr() instead to do this in attr(). Thanks. Its worked.
1

element equal to selector '.id_transp';

  var $element = $(...);
        $element.prop('disabled', true);
        $element.attr('disabled', true); 

        // The following do not require jQuery
        $element.get(0).disabled = true;
        $element.get(0).setAttribute('disabled', true);
        $element[0].disabled = true;
        $element[0].setAttribute('disabled', true);

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.