Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get cursor position in contenteditable div

function cursor_position() {
    var sel = document.getSelection();
    
    var pos = sel.toString().length;
    if(sel.anchorNode != undefined) sel.collapseToEnd();

    return pos;
}

// Demo:
var elm = document.querySelector('[contenteditable]');
elm.addEventListener('click', printCaretPosition)
elm.addEventListener('keydown', printCaretPosition)

function printCaretPosition(){
  console.log( cursor_position(), 'length:', this.textContent.trim().length )
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: loop through javascript array 
Javascript :: js filter array of objects by value 
Javascript :: throttling function in javascript 
Javascript :: extract words from string js 
Javascript :: object in array javascript 
Javascript :: drop down listing in angular form 
Javascript :: how to send header in axios 
Javascript :: how to modify external json file javascript 
Javascript :: click button javascript 
Javascript :: nodejs wait event loop to finish 
Javascript :: socket.io reconnect example 
Javascript :: javascript onclick to another page div 
Javascript :: js form serialize 
Javascript :: get hours and minutes and seconds from date in javascript 
Javascript :: settimeout in a for loop javascript 
Javascript :: html iframe and JS contentwindow 
Javascript :: angular dynamic class 
Javascript :: how to sort an array of objects by a property value in javascript 
Javascript :: vue watch object member 
Javascript :: javascript convert number to string with 2 decimal places 
Javascript :: csrf token in js laravel 
Javascript :: convert string to date using moment 
Javascript :: giving an html element own attribute using js 
Javascript :: how to stop react app in terminal 
Javascript :: javascript get random number 
Javascript :: validate latitude longitude javascript 
Javascript :: javascript get elements that exist in two arrays 
Javascript :: call javascript function after page load complete 
Javascript :: javascript sessionstorage 
Javascript :: jquery remove link href 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =