I am working on a project where users can work around with a list of information. I'll explain with the code below.
<div>
<div id="factmarker1" onmouseup="factFunction(1)">
<b class="fact-num" id="fact-num1">1</b><mark id="fact1">The earth is spherical</mark>
</div>
<div id="factmarker2" onmouseup="factFunction(2)">
<b class="fact-num" id="fact-num2">2</b><mark id="fact2">There is no gravity in space</mark>
</div>
<div id="factmarker3" onmouseup="factFunction(3)">
<b class="fact-num" id="fact-num3">3</b><mark id="fact3">The earth is made up of more water</mark>
</div>
<div id="factmarker4" onmouseup="factFunction(4)">
<b class="fact-num" id="fact-num4">4</b><mark id="fact4">There is a lunar year every four years</mark>
</div>
<div id="factmarker5" onmouseup="factFunction(5)">
<b class="fact-num" id="fact-num5">5</b><mark id="fact5">Every lunar year has 366 days</mark>
</div>
<div id="factmarker6" onmouseup="factFunction(6)">
<b class="fact-num" id="fact-num6">6</b><mark id="fact6">The earth is spherical</mark>
</div>
<div id="factmarker7" onmouseup="factFunction(7)">
<b class="fact-num" id="fact-num7">7</b><mark id="fact7">The earth is spherical</mark>
</div>
</div>
<div id="previewFact"></div>
Now this is pretty straightforward. If a user clicks or highlights any of the facts, an options tab pops up enabled by javascript where the user has options to do a lot of things like copying, saving or sharing each fact. If a user highlights say fact 5 and chooses to save it, the preview prompt shows something like this:
/*
FACT 5
Every lunar year has 366 days
Now here's my problem: I want to be able to allow a user highlight multiple facts at the same time and I can't find a way to do it. If a user finds fact 1 through 4 interesting and highlights it, I want the preview to be something like this:
/*
FACT 1 to 4
1 The earth is spherical
2 There is no gravity in space
3 The earth is made up of more water
4 There is a lunar year every four years
*/
The user should also be able to choose any multiple facts. Also, the facts are called out from a database using PHP, so, I don't know how many facts there are or what the contents are.
I have tried using the window.getSelection()
method. I tried using "fact-num" class so that when I got the content of the highlighted area, I could search for the classes inside it - like so:
function factFunction(factNum) {
var sel = window.getSelection();
var factNumLength = sel.querySelectorAll('.fact-num').length;
if(factNumLength > 1){ //checking if it is a multiple highlight or not
var factStart = sel.querySelectorAll('.fact-num')[0].innerHTML;
var factStop = sel.querySelectorAll('.fact-num')[factNumLength - 1].innerHTML;
var preview = "Fact "+factStart+" to "+factStop;
preview += "<br/>"+sel.toString();
document.getElementById('previewFact').innerHTML = preview;
}else{
var preview = "Fact "+factNum;
preview += "<br/>"+sel.toString();
document.getElementById('previewFact').innerHTML = preview;
}
}
This isn't the entire block of code as it is much longer, but this is the general structure of the entire thing and it's not working. I don't know if I am supposed to use the value of the window.getSelection()
as I am using it.
I would really appreciate any help as I have spent the whole of yesterday and most of today looking for answers.
Thank you
Edit
Just so I'm properly understood,
when highlighted like this:
It should show this: