2

Q: How do i make a field disable and then another one enable when clicking on an anchor tag.

Progress so far: Disabling the field works, but Re-enabling the field is just another click away unfortunately. Also, the second field doesn't get enabled on the click.

// When clicking "Lock your username" that input field should be disabled, 
//and the Comment section should be enabled
$("#lock").click(function() {
  $("#sender").attr('disabled', !$("#sender").attr('disabled'));
  $("#message").attr('disabled', !$("#message").attr('enabled'));

});

//problems right now:
// clicking twice re-enables the field.
// from what i've understood, disabled fields cannot be targeted with event handlers?
//
//
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formInput">
  <input type="text" id="sender" placeholder="Your Name" />

  <a id="lock" style="border:1px solid rgba(000,200,000,0.5);background:#ccc;cursor:pointer;padding:2px;width:150px">Lock your username</a>

  <div id="messages" style="height:100px; border:1px solid #000;width:200px;">
    &nbsp; Messages will go here
  </div>
  <input type="text" id="message" placeholder="write your comment here" autocomplete="off" disabled />
  <input type="submit" id="send" style="border:1px solid rgba(000,200,000,0.5);background:#ccc;cursor:pointer;padding:2px;width:150px" value="send message" />

</form>

4 Answers 4

3

What you could do is remove the handler after the click if you only want it to be done once. Something like this:

var lockHandler = function () {
    $('#sender').attr('disabled', 'disabled');
    $('#message').attr('disabled', null);
    $('#lock').off('click', lockHandler);
}

$('#lock').on('click', lockHandler);

Probably also want to check to make sure there is an actual user name entered before removing it.

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

1 Comment

Thank you so much! Very clever solution!
2

For Jquery 1.6+ you can use

$("input").prop('disabled', true);
$("input").prop('disabled', false);

Comments

2

This should help:

$("#lock").click(function(){
    $("#sender").attr('disabled', !$("#sender").attr('disabled')); 
    $("#message").attr('disabled', !$("#message").attr('disabled')); 
});

Working Fiddle: http://jsfiddle.net/4u5yuu6f/

1 Comment

Thanks for your input but however, if you click multiple times, the disabling just switches.
1

You can use prop() with callback function , this will toggle the disabled property

$("#lock").click(function(e) {
  $("#sender,#message").prop('disabled', function(i, v) {
    return !v;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="formInput">
  <input type="text" id="sender" placeholder="Your Name" />
  <a id="lock" style="border:1px solid rgba(000,200,000,0.5);">Lock your username</a>
  <div id="messages" style="height:100px; border:1px solid #000;width:200px;">
    &nbsp;
  </div>
  <input type="text" id="message" placeholder="comment here" autocomplete="off" disabled/>
  <input type="button" id="send" value="send message" />
</form>

If you want only execute it once then use one()

$("#lock").one('click', function(e) {
  $("#sender,#message").prop('disabled', function(i, v) {
    return !v;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="formInput">
  <input type="text" id="sender" placeholder="Your Name" />
  <a id="lock" style="border:1px solid rgba(000,200,000,0.5);">Lock your username</a>
  <div id="messages" style="height:100px; border:1px solid #000;width:200px;">
    &nbsp;
  </div>
  <input type="text" id="message" placeholder="comment here" autocomplete="off" disabled/>
  <input type="button" id="send" value="send message" />
</form>

1 Comment

Thank you so much for your contribution. However, if you click the "lock your username" multiple times it just switches.

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.