Skip to content

Instantly share code, notes, and snippets.

@geta6
Created August 23, 2012 03:35
Show Gist options
  • Save geta6/3431925 to your computer and use it in GitHub Desktop.
Save geta6/3431925 to your computer and use it in GitHub Desktop.
access to :before or :after objects using javascript
<html>
<head>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<div id='target'></div>
<input id='button' type='button' value='click'>
<script src='script.js'></script>
</body>
</html>
(function () {
var target = document.getElementById('target')
, button = document.getElementById('button');
button.addEventListener('click', function () {
target.setAttribute('data-content', 'this is before content');
}, false);
}());
#target:before {
display: block;
content: attr(data-content);
width: 160px;
height: 90px;
color: white;
background: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment