Enable/Disable input by id

Asked

Viewed 125 times

1

I have two inputs, by automatically clicking they become readonly, I am doing so:

<script>
    $(document).on('click', '#outros,#txtOutros', function() {
        $('#outros').attr('readonly', true);
        $('#txtOutros').attr('readonly', true);
    });
</script>

But I want that when clicking on the #other input only it gets readonly and vice versa.

  • What’s happening at the moment?

  • @Paulohdsousa when I click on #others e.g. both he and #txtOther get readonly.

3 answers

4


Hello,

Do so

<script>
    $(document).on('click', '#outros, #txtOutros', function() {
       $('#txtOutros, #outros').attr('readonly', false); 
       $(this).attr('readonly', true);
    });
</script>
  • Exactly that.

  • @Bia, Explaining what I did, first I put the two as readonly = false and then only let readonly in THIS, ie. in what was clicked.

  • 1

    Simple and practical.

  • @Paulohdsousa understood yes, very good! I’m starting with Jquery now and sometimes I get lost...

2

jQuery <1.9

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

jQuery 1.9+

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

Source

1

Wouldn’t it be better to use the class attribute? For example class="bt":

$(document).on('click', '.bt', function() {
    $('.bt').attr('readonly', false); 
    $(this).attr('readonly', true);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.