I'm trying to get all elements in an iframe that have been mouse selected. At the moment, I'm doing it like this, I get the paragraph and all children inside it.
I'm getting all paragraphs and its children, and then adding them the class .selected in case of mouseup or mousedown.
Copy code
- $("p", $("#"+frame.id).contents()).each(function(){
- $(this).mouseup(function(){
- $(this).find('*').addClass("selected");})
- .mousedown(function(){
- $(this).find('*').addClass("selected");
- });
- });
This way, if I mouseup or down an element, the above functions are called. However, with this only the first and last paragraphs are selected, disregarding the inbetween ones.
Copy code
- <p><span>...</span> <span>..</span> <span>..</span> </p> this on mouseup
- <p> <span></span> </p>
- <p>...</p>
- <p>...</p>this on mousedown
after this, I get all the selected class elements and set their background-color to yellow:
Copy code
- $(".selected", $("iframe").contents()).each(function(){
- $(this).html("<span style='background-color:yellow'>"+$(this).html()+'</span>');
- $(this).removeClass('selected');
- });
My question is if there is a way that allows me to know which elements are being selected (highlighted by the mouse selection), and where in the selected element, the selection starts and ends.
Thanks,
Regards