{"id":1776,"date":"2017-03-22T20:08:58","date_gmt":"2017-03-22T14:38:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1776"},"modified":"2017-03-28T18:36:17","modified_gmt":"2017-03-28T13:06:17","slug":"selecting-manipulating-css-pseudo-elements-using-jquery","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/selecting-manipulating-css-pseudo-elements-using-jquery\/","title":{"rendered":"[ Solved-10 Answers ] CSS &#8211; Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM :<\/label><\/p>\n<p>Is there any way to select\/manipulate CSS pseudo-elements such as ::before and ::after (and the old version with one semi-colon) using jQuery?<\/p>\n<p>For example, the stylesheet has the following rule:<\/p>\n[pastacode lang=\u201dcss\u201d manual=\u201d.span%3A%3Aafter%7B%20content%3A\u2019foo\u2019%20%7D%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>How to change \u2018foo\u2019 to \u2018bar\u2019 using jQuery?<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>You could also pass the content to the pseudo element with a data attribute and then use jQuery to manipulate that:<\/p>\n<h4 id=\"html\">\u00a0<span style=\"color: #ff6600;\"><strong>HTML:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Cspan%3Efoo%3C%2Fspan%3E%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"jquery\"><strong><span style=\"color: #808000;\">\u00a0jQuery:<\/span><\/strong><\/h4>\n[pastacode lang=\u201djava\u201d manual=\u201d%24(\u2018span\u2019).hover(function()%7B%0A%20%20%20%20%24(this).attr(\u2018data-content\u2019%2C\u2019bar\u2019)%3B%0A%7D)%3B%0A\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"css\">\u00a0<span style=\"color: #800080;\"><strong>CSS:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dcss\u201d manual=\u201dspan%3Aafter%20%7B%0A%20%20%20%20content%3A%20attr(data-content)%20\u2019%20any%20other%20text%20you%20may%20want\u2019%3B%0A%7D%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>To prevent the \u2018other text\u2019 from showing up, we could combine this like this:<\/p>\n<p><strong><span style=\"color: #ff6600;\">HTML:<\/span><\/strong><\/p>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Cspan%3Efoo%3C%2Fspan%3E%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"jquery-2\"><span style=\"color: #808000;\"><strong>\u00a0jQuery:<\/strong><\/span><\/h4>\n[pastacode lang=\u201djava\u201d manual=\u201d%24(\u2018span\u2019).hover(function()%7B%0A%20%20%20%20%24(this).addClass(\u2018change\u2019).attr(\u2018data-content\u2019%2C\u2019bar\u2019)%3B%0A%7D)%3B%0A\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"css-2\"><span style=\"color: #800080;\"><strong>\u00a0CSS:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dcss\u201d manual=\u201dspan.change%3Aafter%20%7B%0A%20%20%20%20content%3A%20attr(data-content)%20\u2019%20any%20other%20text%20you%20may%20want\u2019%3B%0A%7D%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<p>Add\/remove a predetermined class :<\/p>\n<p>In this approach, you\u2019ve already created a class in your CSS with a different :after or :before style. Place this \u201cnew\u201d class later in your stylesheet to make sure it overrides:<\/p>\n[pastacode lang=\u201dcss\u201d manual=\u201dp%3Abefore%20%7B%0A%20%20%20%20content%3A%20%22foo%22%3B%0A%7D%0Ap.special%3Abefore%20%7B%0A%20%20%20%20content%3A%20%22bar%22%3B%0A%7D%0AThen%20you%20can%20easily%20add%20or%20remove%20this%20class%20using%20jQuery%20%3A%0A%0A%24(\u2018p\u2019).on(\u2018click\u2019%2C%20function()%20%7B%0A%20%20%20%20%24(this).toggleClass(\u2018special\u2019)%3B%0A%7D)%3B%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<p>You can\u2019t select pseudo elements in jQuery because they are not part of DOM.<br \/>\nBut you can add an specific class to the father element and control its pseudo elements in CSS.<\/p>\n<h4 id=\"jquery-3\"><span style=\"color: #808000;\"><strong>\u00a0jQuery:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20%20%20%20%24(\u2018span\u2019).addClass(\u2018change\u2019)%3B%0A%3C%2Fscript%3E%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"css-3\"><span style=\"color: #800080;\"><strong>\u00a0CSS:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dcss\u201d manual=\u201dspan.change%3Aafter%20%7B%20content%3A%20\u2019bar\u2019%20%7D%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<p>you could also do:<\/p>\n[pastacode lang=\u201dcss\u201d manual=\u201d%24(\u2018head\u2019).append(%22%3Cstyle%3E.span%3A%3Aafter%7B%20content%3A\u2019bar\u2019%20%7D%3C%2Fstyle%3E%22)%3B%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<p>Here is the way to access :after and :before style properties, defined in css:<\/p>\n[pastacode lang=\u201dcss\u201d manual=\u201d%2F%2F%20Get%20the%20color%20value%20of%20.element%3Abefore%0Avar%20color%20%3D%20window.getComputedStyle(%0A%20%20%20%20document.querySelector(\u2018.element\u2019)%2C%20\u2019%3Abefore\u2019%0A).getPropertyValue(\u2018color\u2019)%3B%0A%0A%2F%2F%20Get%20the%20content%20value%20of%20.element%3Abefore%0Avar%20content%20%3D%20window.getComputedStyle(%0A%20%20%20%20document.querySelector(\u2018.element\u2019)%2C%20\u2019%3Abefore\u2019%0A).getPropertyValue(\u2018content\u2019)%3B%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>IF you want to manipulate the ::before or ::after pseudo elements entirely through CSS, you could do it using JavaScript as shown below.<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201djQuery(\u2018head\u2019).append(\u2018%3Cstyle%20id%3D%22mystyle%22%20type%3D%22text%2Fcss%22%3E%20%2F*%20your%20styles%20here%20*%2F%20%3C%2Fstyle%3E\u2019)%3B%0A%0A\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Notice how the <style> element has an ID, which you can use to remove it and append to it again if your style changes dynamically.<\/li>\n<li>This way, your element has its style exactly how you want it through CSS, with the help of JS.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<ul>\n<li>Add a rule to the document with the new content and reference it with a class. depending on what is needed .the class might need an unique id for each value in content.<\/li>\n<\/ul>\n[pastacode lang=\u201dcss\u201d manual=\u201d%24(%22%3Cstyle%20type%3D\u2019text%2Fcss\u2019%3Espan.id-after%3Aafter%7Bcontent%3Abar%3B%7D%3C%2Fstyle%3E%22).appendTo(%24(%22head%22))%3B%0A%24(\u2018span\u2019).addClass(\u2018id-after\u2019)%3B%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<ul>\n<li>Why adding classes or attributes when you can just append a style to head<\/li>\n<\/ul>\n[pastacode lang=\u201dcss\u201d manual=\u201d%24(\u2018head\u2019).append(\u2018%3Cstyle%3E.span%3Aafter%7B%20content%3A\u2019changed%20%09%09%09content\u2019%20%7D%3C%2Fstyle%3E\u2019)%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 9:<\/label><\/p>\n<ul>\n<li>Write separate classes attached with psuedo element for each style and then using JavaScript or jQuery toggle between these classes as shown below.<\/li>\n<\/ul>\n[pastacode lang=\u201dcss\u201d manual=\u201d.green%3A%3Abefore%20%7B%0A%20%20%20%20content%3A%20\u2019green\u2019%3B%0A%20%20%20%20color%3A%20green%3B%0A%7D%0A%24(\u2018p\u2019).removeClass(\u2018red\u2019).addClass(\u2018green\u2019)%3B%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 10:<\/label><\/p>\n<ul>\n<li>Inject new styles to the existing document stylesheet directly either via JavaScript and the webpage will automatically reflect the new css.<\/li>\n<\/ul>\n[pastacode lang=\u201dcss\u201d manual=\u201ddocument.styleSheets%5B0%5D.addRule(\u2018.red%3A%3Abefore\u2019%2C\u2019color%3A%20green\u2019)%3B%0Adocument.styleSheets%5B0%5D.insertRule(\u2018.red%3A%3Abefore%20%7B%20color%3A%20green%20%7D\u2019%2C%200)%3B%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM : Is there any way to select\/manipulate CSS pseudo-elements such as ::before and ::after (and the old version with one semi-colon) using jQuery? For example, the stylesheet has the following rule: [pastacode lang=\u201dcss\u201d manual=\u201d.span%3A%3Aafter%7B%20content%3A\u2019foo\u2019%20%7D%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] How to change \u2018foo\u2019 to \u2018bar\u2019 using jQuery? SOLUTION 1: You could also pass the content [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,1208],"tags":[3814,3819,3816,3823,3820,3824,3825,3821,3822,3826,3818,3815,3817,3813,3827],"class_list":["post-1776","post","type-post","status-publish","format-standard","hentry","category-css","category-jquery","tag-access-the-css-after-selector-with-jquery","tag-changing-css-of-after-pseudoclass","tag-javascript-jquery-select-pseudo-element-after","tag-javascript-get-pseudo-element","tag-javascript-select-pseudo-element","tag-jquery-change-before-content","tag-jquery-change-pseudo-element-css","tag-jquery-css-after-content","tag-jquery-pseudo-element-click","tag-jquery-pseudo-selector","tag-manipulating-css-pseudo-elements-using-jquery","tag-modify-css-of-the-after-pseudo-element-using-jquery","tag-modify-pseudo-element-styles-with-javascript","tag-selecting-and-manipulating-css-pseudo-elements-such-as-before","tag-set-pseudo-element-properties-with-javascript"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1776"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1776\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}