I have a span button that links to a section, when this is clicked it scrolls down to said section. I also have an "onclick" function on the span that is attempting to put the focus on a div in the new section.
For some reason I think the a href link is overriding the function to focus.
I have put a console.log in the function and this logs without fail.
this is the button:
<a href="#navpage"><span id="button" onclick="focusText()"></span></a>
This is the div that I'm attempting to focus:
<div role="textbox" contenteditable="true" id="navtype" class="consoleText" tabindex="0" style="border:none" spellcheck="false"></div>
This is the function I'm calling to focus the div:
function focusText() {
document.getElementById("navtype").focus();
console.log("hello");
};
I also tried doing this to no avail:
document.getElementById("button").onclick = function () {
document.getElementById("navtype").focus();
console.log("hello");
};
I have edited the function and it works when I click on something else e.g. a different div which you can see below:
document.getElementById("navpage").onclick = function () {
document.getElementById("navtype").focus();
};
I'm aware that normally you are unable to focus on a div, however I've got around this by adding a tabindex to the div, which means it is now focusable.