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 content in a p
tag with specific requirements for highlighting:
- User selects a word or phrase within the text
- User is prompted to enter additional information
- The selected words are wrapped in a
span
tag with a custom attribute (data-id
) populated by the user's input from the prompt
How can I implement this functionality?