9

I have to use <a> inside a <label> tag. Because there are many css styles only apply to <a> in our current system. The <a> tag is not linking to any pages but for styling/hovering.

See code below:

<input type="checkbox" id="my-id">
<label for="my-id"><a href="javascript:void(0)">Some text</a></label>

But when clicking on "Some text", it doesn't toggle the checkbox status.

I've tried $.preventDefault()on the <a> tag but doesn't work.

What should i do to make the <a> behaves like a label?

Darshak
  • 859
  • 7
  • 18
Ryan Hu
  • 342
  • 3
  • 18

6 Answers6

5

If you remove the href attribute from the a element, clicking on the text will toggle the checkbox.

<input type="checkbox" id="my-id">
<label for="my-id"><a>Some text</a></label>

As the HTML spec states:

The href attribute on a and area elements is not required

GOTO 0
  • 42,323
  • 22
  • 125
  • 158
3

Updated: this answer were given before the edit where made, which now invalidated it, still, I'll leave it for the ones it can be useful


Give the a anchor pointer-events: none

label[for="my-id"] {
  cursor: pointer;
}
label[for="my-id"] a {
  pointer-events: none;
}
<input type="checkbox" id="my-id">
<label for="my-id"><a href="#">Some text</a></label>

If you need to support older browser, use a pseudo element to cover it

label[for="my-id"] {
  cursor: pointer;
  position: relative;
}
label[for="my-id"]::after {
  content:  ' ';
  position: absolute;
  left: 0; top: 0;
  right: 0; bottom: 0;
}
<input type="checkbox" id="my-id">
<label for="my-id"><a href="#">Some text</a></label>
Asons
  • 84,923
  • 12
  • 110
  • 165
  • This has very limited browser support. – Soviut Feb 04 '17 at 08:04
  • 1
    @Soviut The global support is about 93.58%, you find that limited? – Asons Feb 04 '17 at 08:06
  • On many browsers, especially mobile browsers, it's only just started being supported. Chrome 53 on Android was the first version. Any older versions will still treat it as a link. Besides, the OP wants the style of a link, not an actual href. – Soviut Feb 04 '17 at 08:09
  • @Soviut I added an option for all those ... down to IE8 if the single colon is used on the pseudo class ... and the OP said: _I have to use inside a – Asons Feb 04 '17 at 08:12
  • @LGSon thank you very much. But it also kills onhover behavior which I need for styling in this case. I missed that in original question, already edited. – Ryan Hu Feb 04 '17 at 08:17
3

Do not use <a> only for styling. Use a class on the label instead.

.my-link-style {
  /* To match the cursor style for an <a> */
  cursor: pointer;
  
  /* Add more styles here */
}
<input type="checkbox" id="my-id" />
<label for="my-id" class="my-link-style">Some text</label>
John Bupit
  • 10,406
  • 8
  • 39
  • 75
2

Just put the anchor element on the outside of the label element.

<input type="checkbox" id="my-id">
<a href="javascript:void(0)"><label for="my-id">Some text</label></a>
1

<input type="checkbox" id="my-id">
<label for="my-id"><a href="javascript:void(0)" target="my-id">Some text</a></label>
<script>
  document.querySelector("label[for=my-id] > a")
  .onclick = function(e) {
    var el = document.getElementById(
      this.parentElement.htmlFor
    );
    el.checked = !el.checked;
  }
</script>
guest271314
  • 1
  • 15
  • 104
  • 177
0

You need to reverse, include label in a:

<input type="checkbox" id="my-id" >
<a href="javascript:void(0)"><label for="my-id">Some text</label></a>

JSFiddle

BladeMight
  • 2,670
  • 2
  • 21
  • 35
  • 1
    Main question was: `when clicking on "Some text", it doesn't toggle the checkbox status.` I think my example solves it, who and why downwoted? – BladeMight Feb 04 '17 at 08:16