1

i am posting the following code which is used to disable text fields of table. now how to modify it such that it also disable text fields for fields say id=name=usn.

jQuery(document).ready(function ($) {
// Disabling Text Fields by default
$("table input[type='text']").each(function () {
    $(this).attr("disabled", "disabled");
});

$(".inputradio1 input[name=select1]").change(function () {
    var val = $(this).val();
    if (val == "No") {
        $("table input[type='text']").each(function () {
            $(this).attr("disabled", "disabled");
        });
    } else if (val == "Yes") {
        $("table input[type='text']").each(function () {
            $(this).removeAttr("disabled");
        });
    }
});
});
1
  • you want to disable based on id or name? Commented Jul 27, 2013 at 16:06

3 Answers 3

2

Use prop('disabled', true) instead. There's been a few changes since the jQuery 1.6/1.7 versions to distinguish between attributes and properties. Also, there's no need for the loops, you can just apply it to the whole set of matched elements:

$("table input[type='text']").prop('disabled', true);

Using the following will also make your selector quicker & shorter to read:

$("table :text").prop("disabled", true);

So in the end, what you are left with is:

$("table :text").prop("disabled", true);

$(".inputradio1 input[name=select1]").change(function () {
    $("table :text").prop('disabled', $(this).val() === 'No');
});
Sign up to request clarification or add additional context in comments.

2 Comments

but the field usn is enable til now
I didn't know you could use :text on it's own like that. Nice tip.
1

yes friends i did it...

$("table :text").prop("disabled", true);

$(".inputradio1 input[name=select1]").change(function () {
$("#usn, table input[name='usn']").prop('disabled', $(this).val() === 'No') && $("table :text").prop('disabled', $(this).val() === 'No');
});

1 Comment

ah okay, but you don't need the &&
1

I'm not sure I understand the question.

Use prop() instead.

To disable if id or name is usn.

// id's are suppose to be unique. no need for complex find
$("#usn, table input[name='usn']").prop("disabled",true);

EDIT:

jQuery(document).ready(function ($) {
    // Disabling Text Fields by default
    $("table input[type='text']").prop('disabled',true);
    $("#usn, table input[name='usn']").prop("disabled",true);

    $(".inputradio1 input[name=select1]").change(function () {
        val disable = $(this).val() == "No";
        $("table input[type='text']").prop("disabled",disable);
        $("#usn, table input[name='usn']").prop("disabled",disable);
    });
});

2 Comments

you are correct but i want to merge both...i.e $("#usn, table input[name='usn']").prop("disabled",true); and $("table :text").prop('disabled', $(this).val() === 'No');
@SakirAlam I'm not sure I understand, but is my edit closer to what you need?

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.