I cannot use the CSS pseudo-element with Jquery

Asked

Viewed 77 times

0

I want, when clicking on the pseudo-element, the menu opens, I tried several codes but none worked, the last one I tried was this :

$('#overlay-menu').after().click(function(){
    var menu = $('#overlay-menu'); //Antes eu tentei usar o this, porém, sem sucesso também
    if (menu.className != 'open') {
        menu.className == 'open';
    }
});

1 answer

1


According to this answer in the Soen:

This is not possible; pseudo-Elements are not part of the DOM at all so you can’t bind any Events directly to them, you can only bind to their Parent Elements.

That is to say:

This is not possible, pseudo-elements are not part of the DOM, so you can not delegate events directly to these, you can only delegate to your parents (parents)

However I leave here a better alternative (toggleClass) to what you are doing:

$('#overlay-menu').click(function(){
    $(this).toggleClass('open');
});
.open {
  color: red;  
}
#overlay-menu:after {
  content: '++++'
}

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="overlay-menu">HEYA</p>

  • So I would have to create a span inside the Nav and style it like the after, right ?

  • 1

    Yes I think that would be the solution.. But @Murilogambôa, take a look at the mechanics of adding/removing the class I put in. I think that would be better for you

  • is that as the menu is hidden and there is only one after being shown to open the menu overlay, I think the span would be better even, and I found very good your code :3 thanks for the clarifications

  • 1

    You’re welcome @Murilogambôa, I’m glad I helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.