1

I'm trying to make an input field disabled/enabled when I click on its sibling link.

Here is my code.

$('a').on('click',function(){
    var inp=$(this).siblings('input');
    inp.prop('disabled', false);
    $(this).click(function(){
        inp.prop('disabled', true);
    });
});

When I click first time it works fine, But from next time it won't work. Because the both click functions, triggers. I'm unable to overcome this issue. Please help me.

See the fiddle.

1

4 Answers 4

2

Just negate the disabled prop on every click. elem.disabled = ! elem.disabled.

Demo Fiddle: http://jsfiddle.net/abhitalks/2a2ajbnb/1/

Snippet:

$('a').on('click',function(){
    var $inp = $(this).siblings('input');
    $inp.prop('disabled', !$inp.prop('disabled'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="x" value="22" disabled/><a href="#">change</a>

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

Comments

2

Try this:

$('a').on('click',function(){
    var inp=$(this).siblings('input');
    inp.prop('disabled', !inp.prop('disabled'));

});

Comments

1

You could try making it so one click function does both jobs, that's probably easier. Otherwise, just turn off the previous click function using

$('a').off('click');

Comments

1

$('a').on('click', function() {

  if ($('#ib').attr('disabled') == 'disabled') {
    $('#ib').prop('disabled', false);
  } else {
    $('#ib').prop('disabled', true);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id='ib' type="text" name="x" value="22" disabled/>
<a href="#">change</a>

DEMO

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.