var str = "bar"; document.styleSheets[0].addRule('p:before', 'content: attr(data-before);'); $('p').on('click', function () { $(this).attr('data-before', str); });