HTML:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sit amet est tempus, fermentum ligula et, hendrerit nisl. Praesent tempor eget quam quis auctor. Vivamus mollis mauris id sem aliquam, vel fermentum neque fringilla. Morbi malesuada accumsan augue ut hendrerit. Aenean commodo vulputate lacinia. Integer at purus eget arcu venenatis consectetur vel sed sem. Mauris quis est id ligula aliquam tempor a nec neque. Donec scelerisque velit ac metus aliquet, in euismod nisl lacinia. Quisque imperdiet gravida facilisis. In hac habitasse platea dictumst. Quisque vel erat congue, consequat libero eget, blandit sapien. Suspendisse potenti. Praesent at mollis purus.</p>
CSS:
.highlight {
color: red;
}
JS:
$(function(){
$('p').click(function(){
// get highlighted words
// get user input
var user_input = window.prompt();
// put highlighted words in <span class="highlight"></span> element
// and add data-id to the span element
// expected output:
// <p>... <span class="highlight" data-id="user input here">highlighted words here</span> ...</p>
});
});
I have a block of text in p
tag and I want it to be like this:
- User select some word(s) inside the block
- User enter something inside the prompt box
- Put the highlighted word(s) inside
span
tag with data-attribute (I'd called itdata-id
) and this data-attribute value is coming from the prompt (step 2 above)
How do I achieve this?