JSFiddle - React, Tailwind, and code Playground
HTML
<textarea id="textarea">
Some sample text.
<img src="" alt="">
More text
<img src="" alt="">
</textarea>
<br>
<a onclick="nextAlt($('#textarea'));">Next</a>
CSS
textarea {
width: 400px;
height: 200px;
}
JavaScript
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToPos (input, pos) {
setSelectionRange(input, pos, pos);
}
function nextAlt(input) {
pos = input.val().indexOf('alt=""');
if (-1 == pos) {
alert("None found");
} else {
setCaretToPos(input.get(0), pos);
}
}