How to move the cursor to the end of content with span of a contenteditable element after Focus

Asked

Viewed 133 times

0

I am making an input text using contenteditable, what I need is a Javascript button that when you click Focus in this field, I even managed, but after that I need to put the cursor at the end of the content, for the user to continue editing, so that the operation is equal to an input, motto that within this div may also have Ivs and spans, see what I already have:

$(document).on('click', '.btn', function (e) {
  $('.formfield').focus();
});
.formfield{
	width: 200px;
	height: 30px;
	display: block;
	margin-bottom: 15px;
	border: solid 1px #000000;
	resize: none;
}
.btn{
	width: 80px;
	height: 30px;
  line-height: 30px;
	display: block;
	margin-bottom: 15px;
	background-color: #380303;
  color: #ffffff;
	text-align: center;
  cursor: pointer;
}
<div class="formfield" contenteditable="true">Text <span>aa</span></div>
<div class="btn">get focus</div>

<script src="https://code.jquery.com/jquery-1.12.3.js" integrity="sha256-1XMpEtA4eKXNNpXcJ1pmMPs8JV+nwLdEqwiJeCQEkyc=" crossorigin="anonymous"></script>

  • 1

    @Bacco as you think work with contenteditable, but, I have not tested, I will test, if it does not work I withdraw the comment.

  • 1

    @Bacco really those answers did not correspond with contenteditable.

2 answers

4

Here is a solution using the crease and the Selection :

$(document).on('click', '.btn', function (e) {
    var el = document.querySelector(".formfield");
    var range = document.createRange();
    var sel = window.getSelection();
    range.setStart(el, 1);
    range.collapse(true);
    sel.removeAllRanges();
    sel.addRange(range);
  });
.formfield{
	width: 200px;
	height: 30px;
	display: block;
	margin-bottom: 15px;
	border: solid 1px #000000;
	resize: none;
}
.btn{
	width: 80px;
	height: 30px;
  line-height: 30px;
	display: block;
	margin-bottom: 15px;
	background-color: #380303;
  color: #ffffff;
	text-align: center;
  cursor: pointer;
}
<div class="formfield" contenteditable="true">Texto de exemplo</div>
<div class="btn">get focus</div>

<script src="https://code.jquery.com/jquery-1.12.3.js" integrity="sha256-1XMpEtA4eKXNNpXcJ1pmMPs8JV+nwLdEqwiJeCQEkyc=" crossorigin="anonymous"></script>

This answer is based on this reply soen

  • +1 Sounds good. Can you tell if browser compatibility is good?

  • What I tested works in Firefox, Edge, Chrome and Opera. As for IE’s black sheep, I don’t know. The original author mentions that versions prior to IE 9 will need another code so it will be a no.

  • 3

    Nowadays I would say that attending the 11 is more than good (because it works in Win 7, so there is no excuse to be outdated.

  • Thank you very much, but a problem has arisen, it works only if you have a text content.... but if you have a <span>, it doesn’t go all the way. I’ll edit my question

2


Lazyfox’s answer is almost correct, but the child elements affect the cursor, in case the range.setStart(el, 1); will move the cursor on the parent, but any Element (textNode "does not count") inside also has to go through the behavior, it turns out that the cursor is barred in the first child element it finds, so what you can do is take the last child HTML element (if it exists) and apply the setStart in it, example:

$(document).on('click', '.btn', function (e) {
    var el = document.querySelector(".formfield");

    if (el.lastElementChild) el = el.lastElementChild; //se existir elementos

    var range = document.createRange();
    var sel = window.getSelection();
    range.setStart(el, 1);
    range.collapse(true);
    sel.removeAllRanges();
    sel.addRange(range);
  });
.formfield{
	width: 200px;
	height: 30px;
	display: block;
	margin-bottom: 15px;
	border: solid 1px #000000;
	resize: none;
}
.btn{
	width: 80px;
	height: 30px;
  line-height: 30px;
	display: block;
	margin-bottom: 15px;
	background-color: #380303;
  color: #ffffff;
	text-align: center;
  cursor: pointer;
}
<div class="formfield" contenteditable="true">Texto de <b>TESTE</b></div>
<div class="btn">get focus</div>

<script src="https://code.jquery.com/jquery-1.12.3.js" integrity="sha256-1XMpEtA4eKXNNpXcJ1pmMPs8JV+nwLdEqwiJeCQEkyc=" crossorigin="anonymous"></script>

  • Great, that’s all we need

  • 1

    If the last word is a space, or &nbsp; it does not recognize... would have as?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.