1

I have prepared a Fiddle, please look into this.

https://jsfiddle.net/s7fxy8jp/

From the fiddle, would it be possible to blink normal cursor at the end of the text Element Z (script-only focusable) instead of a background color aqua.

HTML

<div tabindex="-1" id="scripted">Element Z (script-only focusable)</div>
<div id="test">Set Focus To Element Z</div>

CSS

div:focus {
background-color: Aqua;}

JavaScript

document.getElementById('test').onclick = function () {
document.getElementById('scripted').focus();};
Soma
  • 31
  • 6

3 Answers3

0

You need to allow your div content to be edited, by using the contenteditable property.

<div tabindex="-1" id="scripted" contenteditable="true">Element Z (script-only focusable)</div>
<div id="test">Set Focus To Element Z</div>

This will get you a blinking cursor on click. Unfortunately it is placed at the beginning of the line. You can solve this problem by following the instructions here.

Here's your Updated JSFiddle

Community
  • 1
  • 1
xShirase
  • 11,975
  • 4
  • 53
  • 85
0

I updated your JavaScript function to activate attribute contenteditable, then move caret to end of text. The text can only be edited when it is focus from "Set focus to element Z", otherwise, it is still a normal div. Note that I used jQuery for faster manipulation, the solution can still be implemented using pure JavaScript

JavaScript

document.getElementById('test').onclick = function () {
    var char, sel,
        $div = $('#scripted'),
        div = $('#scripted')[0];
    $div.attr('contenteditable', 'true');
    $div.focus().blur(function() {
        $div.attr('contenteditable', 'false')
    });
        char = $div.html().length; // character at which to place caret  content.focus();
    sel = window.getSelection();
    sel.collapse(div.firstChild, char);
};
ntahoang
  • 441
  • 5
  • 17
0

Try this

Html

<div tabindex="-1" id="scripted" class="sj">Element Z (script-only focusable)</div>
<div id="test">Set Focus To Element Z</div>

Css :

.sj:hover {background: yellow;}
Sujithrao
  • 789
  • 3
  • 12
  • 27