hsw4840
hsw4840

Reputation: 231

How to remove attribute "readonly" Using JQuery

I'm implementing any bulletin Free board pages applied Secret and non-secret by the board's page password number.

I want to implement if the checkbox that distinguish whether The writings are secret is checked, I want to remove readonly attribute of the password for applying the secret bulletin.

The password's id = bbs_password

the checkbox id = secret_yn

because of all source are my privacy, I can't attach it on this directly. Please understand me.

alternately, I attach a part of sourced codes by photo.

This is what I have so far :

$(document).ready(function(){
$('#bbs_password').attr('readonly','readonly')


if($('input:checkbox[id="secret_yn"]').is(":checked") == true){
    $('#bbs_password').removeAttr('readonly','readonly');
    if(document.bbs_write_form.bbs_password.value == ""){
        alert("input the password!); 
        document.bbs_write_form.bbs_password.focus();
        return false;
    }
}

enter image description here enter image description here thanks very very much for reading this.

Upvotes: 1

Views: 9843

Answers (2)

Vojtech Ruzicka
Vojtech Ruzicka

Reputation: 17075

To set element as read-only:

$('#bbs_password').prop('readonly', true);

And to switch it back to editable:

$('#bbs_password').prop('readonly', false);

If you have jQuery version older than 1.9, you need to use instead:

$('#bbs_password').attr('readonly', true);

Upvotes: 3

Carnaru Valentin
Carnaru Valentin

Reputation: 1845

Depend on your jQuery version try this:

$('.selector').prop('readonly', false);
Or
$('.selector').attr('readonly', false);

Upvotes: 0

Related Questions