Frozen
Frozen

Reputation: 97

enable and disable textbox by using button click

I have multiple of textbox...each textbox have own button to enable or disable . How to do it ? I have already try this

$('#disablebutton').click(function(){
$('#textfieldToClose').attr('disable');
});

<input type="text" name="text11" readonly="readonly" id="textfieldToClose">
<input type="button" value="edit" name="button1" id="disablebutton">

Upvotes: 1

Views: 8535

Answers (3)

Wilfredo P
Wilfredo P

Reputation: 4076

Try:

$(document).ready(function(){
    $('#disablebutton').click(function(){
    if($('#textfieldToClose').prop('disabled'))
    {
     $('#textfieldToClose').prop('disabled', false)
    }
    else{
         $('#textfieldToClose').prop('disabled', true)
      }
    });
})

or with Read only:

$(document).ready(function(){
    $('#disablebutton').click(function(){
    if($('#textfieldToClose').prop('readonly'))
    {
     $('#textfieldToClose').removeAttr('readonly');
    }
    else{
         $('#textfieldToClose').attr('readonly', 'readonly')
      }
    });
});

Because you only select the attr but, don't do anything with. Live demo

Upvotes: 2

Akinkunle Allen
Akinkunle Allen

Reputation: 1309

$('#disablebutton').click(function(){
  $('#textfieldToClose').attr('disabled', 'disabled'); //sett the `disabled` attribute on the    element
});

$('#textfieldToClose').attr('disable') did not work because you were just quering an attribute disable on the element which doesn't even exist. The correct name is disabled

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

Try this:

$('#disablebutton').click(function(){
   $(this).prev().attr("disabled", "disabled"); 
});

Working Demo

Upvotes: 1

Related Questions