HTML Create link and focus on it

136
October 10, 2019, at 9:00 PM

I want to insert <a> tag into the HTML. I did some search, and I find this solution.

var range = document.getSelection().getRangeAt(0);
var nnode = document.createElement("a");
range.surroundContents(nnode);
nnode.innerHTML = url;
nnode.setAttribute(url);

It works, but the only problem is that, after inserting, the link auto selected. Is there a way to put the cursor into the end of the URL? I tried nnode.focus(), but it doesn't work..

Thanks!

UPDATE: One Option I thought is to trigger a "right arrow" key event. Is there a simple way to tiger that key event?

Answer 1

For "tiger" that key event:

document.onkeydown = function(e) {
    switch (e.keyCode) {
        case 37:
            alert('left');
            break;
        case 38:
            alert('up');
            break;
        case 39:
            alert('right');
            break;
        case 40:
            alert('down');
            break;
    }
};
Rent Charter Buses Company
READ ALSO
Showing one article element and hiding others at the same time using JQuery

Showing one article element and hiding others at the same time using JQuery

I have three internal links on HTML page and when I click them they show an article element for a while, which has some textEach link has a separate article element (and other article elements should be hidden, when that one is shown)

95
How to prevent touching the view until a view dismiss animation function ends?

How to prevent touching the view until a view dismiss animation function ends?

I have a custom java class which extends FrameLayout that sits ontop of another custom parent view that extends FrameLayout

122
Periodic work requests skipped with WorkManager in Andorid

Periodic work requests skipped with WorkManager in Andorid

I implemented some repetitive task using WorkManager and it works just fine (runs my code each period with couple minutes sooner or later) in day time when I actively use my phoneOn night time it skips a lot of periods and I wonder why this happen and how to solve this

146
android sync error at Android Studio said fastutil.jar couldn&#39;t download

android sync error at Android Studio said fastutil.jar couldn't download

When ever I create a project, while syncing it gets an error

232