How can I get selected text on a element click event?

Yahya Altintop

I can get selected text on button click event. But i want get it on a element click event.

Here is my code. Thanks advance

function GetSelectedText(){
    console.log(getSelection().toString());
}
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Laborum, nam!</p>
<a role="button" id="a" onclick="GetSelectedText()">a element</a>
<button type="button" id="button" onclick="GetSelectedText()">Button</button>

slashroot

You can achieve this by adding an event to your a tag this way.

function GetSelectedText(){
  selectedText = getSelection().toString();
  console.log(selectedText);
}

document.getElementById("a").addEventListener("mousedown", function(event) {
    event.stopImmediatePropagation();
    event.preventDefault();
});
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Laborum, nam!</p>
<a role="button" id="a" onclick="GetSelectedText()">a element</a>
<button type="button" id="button" onclick="GetSelectedText()">Button</button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the click event inside an innerHTML?

How can I get selected JScrollPane element?

How can I trigger the click event of another element in ng-click using angularjs?

How can I get an output by click on a select2 element?

how can i get particular div text from selected li

How can I prevent triggering an on click event when the actual mouse click didn't start on the target element?

Highcharts - How can I get the nearest point on click event

I can't seem to get the text to update after the click event

Material React Select Multiple: How i can remove from my array when i click in a selected element?

How can I get selected text in pdf in Javascript?

How can I get the element that is clicked on with a click handler added in a forEach?

How can I add an on click event on a div with multiple element inside

How can i get GWT HTML selected text?

How I can get a click event

how to get click event of li element in jquery?

How can I bind click event in repeater element in winjs?

How to get the selected element in jQuery event delegation?

How can I get a listbox's selected item binded element?

how can I make the click event correspond to the selected?

How can I animate a .text change within an on click event?

How i can emit event from directive by click outside of element?

How can i send text from element with id to function on click?

How can i get mouse click event element in cocoa

How can I get on click event on PreferenceCategory's tilte?

How can I get selected value in select dom element [Laravel]

How can I click on the children element in the selected element in the cypress?

How can I find and click button element in cypress by the text on the button

How can I click a dropdown-item button in html code, then get the text of the button in javascript on click event?

How can I use Cypress to check the text of an element's event?