5
I am formatting a document for printing and need to add a page break + header and footer at certain coordinates, if the coordinate is in the middle of the paragraph I need to divide it. I can already make the insertions by manually entering the position where the paragraph divides.
In my initial approach I tried to get the word in the coordinate (x,y) and then the indexOf()
of that word in relation to the paragraph. With "Indice" I can integrate with the existing code.
The closest I could get was to use the function document.elementFromPoint(x,y)
, that returns the entire paragraph.
Below the test code. I am using the mouse click event to simulate the coordinates. In my implementation only calculated coordinates will be used.
document.addEventListener('click', function(event) {
alert(document.elementFromPoint(event.clientX, event.clientY).textContent);
}, false);
div {margin: 15px 0;}
p {margin: 0;}
<div>
<p>Cras vel erat sit amet eros posuere volutpat nec in massa. Quisque dignissim mollis aliquet.</p>
<p>Fusce suscipit rhoncus mi a dapibus. Donec nisl augue, molestie sed porttitor id, pulvinar tempor neque.</p>
<p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec et enim eleifend velit faucibus consequat vel at risus.</p>
</div>
<div>
<p>Aliquam erat volutpat.</p>
<p>Phasellus ornare feugiat convallis. Aenean tincidunt tristique sem eu porttitor. Aliquam convallis eu purus et venenatis.</p>
<p>Suspendisse euismod ullamcorper odio, ac sollicitudin quam pharetra sed. Vestibulum dictum cursus sollicitudin.</p>
<p>Praesent at odio nisi. Interdum et malesuada fames ac ante ipsum primis in faucibus. Mauris sodales vehicula neque quis imperdiet. Morbi lacinia libero in posuere porttitor. Curabitur pretium vel tortor in aliquet.</p>
</div>
<div>
<p>Integer eu sapien odio. Morbi blandit nibh leo, in dapibus sem malesuada vitae. Etiam sit amet tristique sem.</p>
<p>Sed mattis lectus lorem, at dapibus leo suscipit quis. Nunc in massa quis mauris suscipit gravida. Sed at nunc mauris. Duis et lectus ex.</p>
</div>
What if you wrap each word in a span? Then the element returned by that code of yours would be the word. You can calmly do this "package" by JS, I think you already have a question here on the site about this. This one can be a starting point: http://answall.com/questions/41477/comordestrinchar-um-texto-qualquer-no-momento-do-echo-transform-cada-pal/41479#41479
– bfavaretto
@bfavaretto what I need is the position of the word in the paragraph and not the word itself. Use
<span>
seems a good alternative, really can be a starting point– Pedro Sanção
With each word in a span, you get the coordinates of any of them with
Element.getBoundingClientRect()
.– bfavaretto
Another answer in Soen suggests create a span for each word, half the wave the bfavaretto suggested. Would that be an option? What do you intend to do? color what was clicked?
– Sergio
@bfavaretto the function
getBoundingClienteRect
does the opposite of what I need, it does element->coordinate. @Sergio added the expected result to the question– Pedro Sanção
Ah, I had misunderstood your previous comment. So that’s the method you’re using,
elementFromPoint
. As long as the word is alone inside an element (the span I suggested).– bfavaretto