javascript - Automatically tick checkbox when clicking on another form input field - Stack Overflow

I'm trying to have a checkbox next to a form text-input field. The checkbox can be ticked onoff n

I'm trying to have a checkbox next to a form text-input field. The checkbox can be ticked on/off normally, however when clicking on the text-input field, the checkbox should be automatically ticked as well.

I tried this with putting the text-input inside the label for the checkbox, but it doesn't work. It works fine when I use normal text instead of the input-field:

<input type="checkbox" id="box">
<label for="box">
  <input type="text">
</label>

How can I achieve this with HTML/JS? I'm working in the context of a VueJS plugin.

I'm trying to have a checkbox next to a form text-input field. The checkbox can be ticked on/off normally, however when clicking on the text-input field, the checkbox should be automatically ticked as well.

I tried this with putting the text-input inside the label for the checkbox, but it doesn't work. It works fine when I use normal text instead of the input-field:

<input type="checkbox" id="box">
<label for="box">
  <input type="text">
</label>

How can I achieve this with HTML/JS? I'm working in the context of a VueJS plugin.

Share Improve this question asked May 29, 2020 at 15:07 KlausetteKlausette 4122 gold badges5 silver badges14 bronze badges 2
  • Does this answer your question? How to check a checkbox on focus of Input text in table – rxdue Commented May 29, 2020 at 15:11
  • Does this answer your question? How to create a checkbox with a clickable label? – 0stone0 Commented May 29, 2020 at 15:11
Add a ment  | 

6 Answers 6

Reset to default 2

The click action of <input> (focus & start editing) overrides <label>'s, so you'll have to use JS:

document.querySelector('#text').addEventListener('click', ()=>{
  document.querySelector('#box').checked=true
})
<input type="checkbox" id="box">
<input type="text" id="text">

you can use jquery to achieve this:

HTML:

<input type="checkbox" id="box">
<label for="box"></label>
<input type="text" id="mytextinput">

JQuery:

$('#mytextinput').focus(function(){
$('#box').prop( "checked", true ); // true checks the checkbox false unchecks.
});

Simply add a listener to the text input that checks the box.

const checkbox = document.querySelector('#box');
const input = document.querySelector('input[type="text"]');

input.addEventListener('click', () => {
  checkbox.checked = true;
});
<input type="checkbox" id="box">
<label for="box">
  <input type="text">
</label>

You can do this easily by setting an onclick attribute on text field like:

<input type="checkbox" id="box">
<label for="box">
  <input type="text" onclick="box.checked = true">
</label>

    document.querySelector("#box").addEventListener('click',function(){
        document.querySelector("#checkbox").checked = true;
    });
    <div>
        <input type="checkbox" id="checkbox">
        <input type="text" id="box">
    </div>

just write it like this (worked for me):

<label>
   <input type="checkbox" id="box">Feeling lucky
</label>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745297574a4621234.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信