0

I'm completely new to Backbone.js and jQuery and I'm trying to set an input field of a form to "disabled" when I check a checkbox. I'm not sure if I have to do it the way I show in the code below, if so what should I do in the disableInput function?

JS

events: {
    'change #myCheckbox': 'disableInput',
},

disableInput : function(){
...
}

HTML

<div class="x">
      <input type="text" id="myInput">
      <input type="checkbox" id="myCheckbox">
<div>

EDIT: This may seem like dupplicate of other posts, but those questions are about vanilla JS I'm using Backbone here so it's not the same.

3

3 Answers 3

2

If you want to go the pure CSS route, you can place the checkbox above the text and switch the order to your liking using flexbox. Then, you can write CSS that changes the opacity and pointer-events for the checkbox sibling.

.x {
  display: flex;
}

.x #myCheckbox {
  order: 2;
}

.x #myCheckbox:checked + #myInput {
  pointer-events: none;
  opacity: 0.6;
}
<div class="x">
  <input type="checkbox" id="myCheckbox">
  <input type="text" tabindex="-1" id="myInput">
</div>

Tab Issue

In order to stop someone from tabbing into the input, you can add tabindex="-1" to the input.

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

2 Comments

Was just testing it, but if you add tabindex="-1" to the input it will stop the tabbing. NVM :P
I just googled it and found the same solution, haha xD
1

Here is a simple one liner Granted I am not familiar with backbone.js but you should be able to figure out how to incorporate this.

function disableInput(){
document.getElementById("myInput").disabled=document.getElementById('myCheckbox').checked
}
<div class="x">
      <input type="text" id="myInput">
      <input type="checkbox" onchange="disableInput();" id="myCheckbox">
<div>

3 Comments

This is a Backbone question, using vanilla JS and inline onchange event is really going in the wrong direction.
@Emile Bergeron I only used the onchange for the purpose of the snippet. hopefully the poster can figure out how to use this best. Maybe someday i will add backbone to my toolbox :)
@happymacarts I accepted your answer because it essencially works, but as Emile Bergeron said I'm using Backbone so I will have to see how to get the element by id things the way Backbone does it, just to do it "more elegantly". For the inline onchange i just replaced it for the code i posted of my JS.
0

I think Backbone way would be the following

events: {
    "change #myCheckbox": function() {this.disableInput.apply(this, arguments);}
},

disableInput: function(args) {
    var checked = $(args.target).is(":checked");
    this.$el.find("#myInput").disabled = checked;
}

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.