Button inside a label

后端 未结 5 1054
后悔当初
后悔当初 2021-02-12 15:20

I have a label with \"for=\"the pointer to the checkbox input\"\" and as long as I know, this for can be added only for label

5条回答
  •  终归单人心
    2021-02-12 15:43

    HTML:

    
    

    JS:

    const onClickHandler = (e) => {
        if(e.target!==e.currentTarget) e.currentTarget.click()
    }
    

    Target is the click target, currentTarget is the label in this case.

    Without the if statement the event is fired twice if clicked outside of the event preventing area.

    Not cross browser tested.

提交回复
热议问题