I am basically trying to replicate the .selectable() method of jQuery but I need it in vanilla JavaScript. Which event listener method should I use? I've tried with mousedown, but how can I detect that another element has been visited?
Here is an example on how jQuery does it: [http://jsfiddle.net/ZfevM/99/]
How to click on an element, move the mouse over other elements and then select those elements that you hovered over with JavaScript?
337 views Asked by SirFredy At
2
There are 2 answers
0
Dmitriy Godlevski
On
You could make use of window.getSelection() and [selection].getRangeAt([index]);
document
.getElementsByTagName('body')[0]
.addEventListener('mouseup',
function(){
const selection = window.getSelection();
const range = selection.getRangeAt(0);
console.log( selection, range )
})
Selection has .containsNode() function to compare against given set; Range has .startContainer and .endContainer along with .commonAncestorContainer to compare against given structure
Related Questions in JAVASCRIPT
- Using Puppeteer to scrape a public API only when the data changes
- inline SVG text (js)
- An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
- Storing the preferred font-size in localStorage
- Simple movie API request not showing up in the console log
- Authenticate Flask rest API
- Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
- How to request administrator rights?
- mp4 embedded videos within github pages website not loading
- Scrimba tutorial was working, suddenly stopped even trying the default
- In Datatables, start value resets to 0, when column sorting
- How do I link two models in mongoose?
- parameter values only being sent to certain columns in google sheet?
- Run main several times of wasm in browser
- Variable inside a Variable, not updating
Related Questions in EVENTS
- Stop propagation of javasript/leaflet click event that starts in one element and ends in another
- Detecting click inside and outside of the listening component in Angular
- How to use mocha unit test chokidar watch events
- writing event_management in unity
- Where to put event handler method of an inherited class in C++ Builder?
- Event_date reference in CTE
- WinForms, event unable to subscribe from a custom class
- How to intercept a request made by a form submit in JavaScript?
- Communicating from Parent to Child in Blazor
- How to Customize Sitecore Copy operation
- grand parent is handling custom event emitted instead of parent in Angular 17
- jquery not capturing all input value changes
- Is there a way to force the focus on a determined window tab?
- How to watch user browsing activity from a background service?
- Trigger Once in React native
Related Questions in MOUSEEVENT
- Detect Mouse Clicks; Terminate Program on Scroll Wheel Movement
- how to create a mouseEvent with target in typescript without dispatching?
- How to make (mulitple) image appear when clicking?
- An HTML Link Element ("a") Refuses to Attach Itself to Event Listener
- "contextmenu" Event interrupts "onmousemove" from executing a function
- right click event for wxDataViewListCtrl
- How to save the brush event when hovering over elements with their own events
- Mouse input not blocked when running Python code on Ubuntu using pynput library
- createJS does not reconize currentTarget
- How To Make Mouse Recoil be smooth?
- What's the benefit of multiline code with multple variables compared to combined actions pygame.mouse.getpos() that combine them
- How can I prevent the behavior of swipe backward of browser on mac?
- Adding a circle which follows a mouse and changes colours based on the current colour | Interactive SVG Colouring Page | Using JavaScript
- How to move the background image by clicking and dragging the mouse smoothly?
- How to add an event listener to elements (or children)
Related Questions in EVENT-LISTENER
- Using two buttons with onclick events that continuously change each other?
- How to properly use Spring AbstractEntityManagerFactoryBean.setEntityManagerInitializer method?
- Why isn't my JS eventlistener triggering a function?
- Drawing a node in click position using Vis.js
- event listener for required inputs other than keyup semi working
- How can I programmatically click on elements using custom event listeners?
- Get cursor position relative to element, ignoring children
- Get attribute of target and not parent in forEach loop
- undefined parameter in JavaScript
- Event listener not being triggered
- How to send data from parent(outlook add-in) window to a pop up window in javascript?
- Use EventListener causing circular dependency with repository
- I do not understand why my timer is stopping on javascript keydown event. That is what I want but I just do not know why it is stopping
- Add persistent global function across pages via window, or localstorage, etc
- Selecting last entry of e.EventData["documentURL"] in a DevTools event listener
Related Questions in JQUERY-UI-SELECTABLE
- How to place rectangles to selected div corners?
- Why is my span textarea bigger then the characters itself
- In R Shiny, is it possible to select a group of check boxes by click-and-dragging over them?
- Make selectable buttons in Flutter
- Can't create a simple selectable tag view in SwiftUI
- Flutter: How to fix SelectableText scroll error in flutter
- Flutter selectable text on all table
- How to add `overflow` to SelectableText in Flutter?
- Does abybody know how to unselect Jquery Selectable by clicking outside selected object?
- Having jQuery UI Selectable both as grid and a list in a table
- Selectable() Jquery mobile alternative
- How make sortable only for one child
- Jquery Ui: Selectable - possible to select two list items with one click?
- Is there an HTML DOM equivalent to JqueryUI selectable?
- How to click on an element, move the mouse over other elements and then select those elements that you hovered over with JavaScript?
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
I was able to figure it out. Had to work with
mouseupandmousemoveas well. Here is what I came up with and it works as expected:[https://repl.it/repls/EquatorialLumberingInfinity]Any thoughts or alternative solutions are welcomed!