1

Can anyone help me to set top position for $myClass before and after. Below is my code that I tried.

var $myClass = $(".myclass");
$myClass.filter(":before,:after").css("top",-23); 
prakashstar42
  • 677
  • 1
  • 6
  • 16
  • You cannot select pseudo elements like `:before` through jQuery. – Shaunak D May 21 '15 at 05:45
  • 2
    you can add another class to your css with :before and after then use $myClass.addClass('newclass'); or $myClass.removeClass('newclass'); or $myClass.toggleClass('newclass') – Mohamed-Yousef May 21 '15 at 05:47

1 Answers1

0

Try appending a style element to document , including properties , values for .myclass:before , .myclass:after at style text

$("<style>", {"text":".myclass:before,.myclass:after{top:-23}"})
.appendTo("body");

See also https://stackoverflow.com/a/27255808/

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • Whats is the difference between this one and yours. \\ $(".myclass:after,.myclass:after").css("top",-23); \\ – prakashstar42 May 21 '15 at 05:51
  • @prakashstar42 `$(".myclass:after,.myclass:after")` does not select the `pseudo` elements . Appending a `style` element to document sets properties for `.myclass:before` , `.myclass:after` `css` selectors – guest271314 May 21 '15 at 05:57
  • @prakashstar42 See http://dev.w3.org/csswg/css-pseudo-4/ _"Pseudo-elements represent abstract elements of the document beyond those elements explicitly created by the document language."_ ; see also http://nicolasgallagher.com/an-introduction-to-css-pseudo-element-hacks/ _"As their names imply, they are not part of the DOM in the way that ‘real’ HTML elements and classes are. Instead, they are CSS abstractions that provide additional, and otherwise inaccessible, information about the document."_ – guest271314 May 21 '15 at 06:07
  • @prakashstar42 Tried `js` at post ? – guest271314 May 21 '15 at 06:20
  • Thanks for your reply. I have used addClass property to solve this. Thanks again – prakashstar42 May 21 '15 at 06:23