Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created April 25, 2012 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/2493590 to your computer and use it in GitHub Desktop.
Save xeoncross/2493590 to your computer and use it in GitHub Desktop.
Convert DOM element words into click-able list of spans
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var text = $("#select_words").text().trim().split(" ").join("</span> <span>");
$("#select_words").html("<span>" + text + "</span>");
$("span").click(function () {
var $p = $(this).parent('div');
console.log($(this).prev().html());
console.log($(this).html());
console.log($(this).next().html());
$(this).remove();
console.log($p.text());
});
$('span').hover(function()
{
console.log($(this).html());
});
});
</script>
<style type="text/css">
span { background: #ccc; }
</style>
</head>
<body>
<div>
This is normal text.
</div>
<div id="select_words">
This text is selectable and click-able.
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment