0

I'm trying to set a textbox to 'readonly', add a class, and put a text into the textbox at that moment when I check the checkbox. Moreover, I'm also trying to remove 'readonly' attribute from the textbox, add a class, and delete text in the textbox.

I have

$('#CheckBoxSectionCode').click(function () {
    if ($(this).is(':checked')) {
        $('#TextBoxSectionCode').attr('readonly', 'readonly');
        $('#TextBoxSectionCode').addClass('disabled');
        $('#TextBoxSectionCode').text(document.getElementById('TextBoxSectionName').val);
        }
    else {
        $('#TextBoxSectionCode').attr('readonly', false);
        $('#TextBoxSectionCode').addClass('abled');
        $('#TextBoxSectionCode').text('');
    }
});

This code doesn't work for me.

Thanks,

Phillip


Thanks everyone for answers.

According to your comments and answers, I've changed my code but it's still not working.

$('#CheckBoxSectionCode').click(function () {
    if ($(this).is(':checked')) {
        $('#TextBoxSectionCode').prop('readonly', true);
        $('#TextBoxSectionCode').addClass('disabled');
        $('#TextBoxSectionCode').text('disabled');

    }
    else {
        $('#TextBoxSectionCode').prop('readonly', false);
        $('#TextBoxSectionCode').removeClass('disabled').addClass('enabled');
        $('#TextBoxSectionCode').text('');
    }
});

I'm using chrome browser to run this code, and using developer tools in chrome and put a break point at the code above to see what's happening in the jquery. However, when I click the check box to check/uncheck, nothing happens there.

5
  • 2
    Just to clarify; when @SterlingArcher says caching the jQuery object, she/he means doing something like var obj = $('#TextBoxSectionCode') then calling the functions using the variable like this: obj.attr(...); obj.addClass(...). Every time you do a $(something) you are calling a function in jQuery that looks for the DOM. Commented Apr 6, 2015 at 19:42
  • 2
    @SterlingArcher No problem. I think your comment is quite solid. You should make it into an answer :) Commented Apr 6, 2015 at 19:43
  • 1
    I did, with your credited explanation. Thanks Commented Apr 6, 2015 at 19:45
  • @PhillipHG and it's better to use prop('readonly', true/false) over attr() - api.jquery.com/prop just my two cents Commented Apr 6, 2015 at 19:47
  • I edited but still not working. please see my edit. Commented Apr 6, 2015 at 20:19

4 Answers 4

1

document.getElementById('TextBoxSectionName').val this is wrong. You really should cache your jQuery object so it's not navigating the DOM over and over. Then you mix in native JS and .val is not a DOM property or method, nor is it a jQuery property, it should be .value for a DOM object or .val() for a jQuery object.

Obligatory explanation by @Archy Wilhes:

"Just to clarify; when @SterlingArcher says caching the jQuery object, she/he means doing something like var obj = $('#TextBoxSectionCode') then calling the functions using the variable like this: obj.attr(...); obj.addClass(...). Every time you do a $(something) you are calling a function in jQuery that looks for the DOM."

Sign up to request clarification or add additional context in comments.

1 Comment

Very good explanation. My own addition, just so it doesn't sound poisonous to try mixing JQuery and native JS: document.getElementById will return an actual HTMLElement. $("#idIsHere") will return a JQuery object; which, on a basic level, is an array containing any number, but usually one, HTMLElement. It also has ten billion helper functions on it to perform various operations on every HTMLElement inside of it. You can retrieve the native element with jqObj[0], and you can create a JQuery object with $(myHtmlElementVar) (no quotes in the call).
0

since everytime you are adding the class the element is going to end up having both the two classes. Consider removing the other class before adding one. For example,

$(selector).removeClass('disabled').addClass('enabled')

Comments

0

Try with change event instead of click:

    $('#CheckBoxSectionCode').change(function () {
        if ($(this).is(':checked')) {
        $('#TextBoxSectionCode').attr('readonly', 'readonly');
        $('#TextBoxSectionCode').addClass('disabled');
        $('#TextBoxSectionCode').text(document.getElementById('TextBoxSectionName').val);
    }
    else {
        $('#TextBoxSectionCode').attr('readonly', false);
        $('#TextBoxSectionCode').addClass('abled');
        $('#TextBoxSectionCode').text('');
    }

});

Comments

0

You could do the following way.

//Cache reference to DOM as DOM scan is expensive!
var textBox = $('#TextBoxSectionCode');
$('#CheckBoxSectionCode').click(function () {
    //Use prop as opposed to attr
    textBox.prop("readOnly", false).removeClass('disabled').addClass('abled').text("");
    if ($(this).is(':checked')) {
        textBox.prop("readOnly", true).removeClass('abled').addClass('disabled').text($("#TextBoxSectionName").val());
    }
});

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.