15

Lets say I have:

<div></div>

with:

div {
    width: 500px;
    height: 500px;
    position: relative;
}
div:after {
    content: '';
    display: block;
    width: 20px;
    height: 20px;
    position: absolute;
    top: 0;
    right: 0;
}

Can I do something like $('div:after').click(function(){...}); ? And have it not fire if I am clicking on div but not div:after.

dezman
  • 18,087
  • 10
  • 53
  • 91

1 Answers1

8

Maybe. Depending on how you structure your pseudo content you would have to rely on calculating mouse position for clicks on the actual div. The event handlers would all go on the div, and not the pseudo element because it doesn't exist in the DOM.

See Manipulating CSS :before and :after pseudo-elements using jQuery for some more info. Especially BoltClock's answer.

Also see Felix's comment for another possible solution without mouse position: Only detect click event on pseudo-element

Community
  • 1
  • 1
ryan
  • 6,541
  • 5
  • 43
  • 68