Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Solution 1: Find next empty <code>alt</code>-attribute</h2> <p><strong>JavaScript</strong></p> <pre><code>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); } } </code></pre> <p><strong>HTML</strong></p> <pre><code>&lt;textarea id="textarea"&gt; Some sample text. &lt;img src="" alt=""&gt; More text &lt;img src="" alt=""&gt; &lt;/textarea&gt; &lt;br&gt; &lt;a onclick="nextAlt($('#textarea'));"&gt;Next&lt;/a&gt; </code></pre> <p><strong>Demo</strong></p> <p><a href="http://jsfiddle.net/rMqbW/" rel="nofollow noreferrer">http://jsfiddle.net/rMqbW/</a></p> <p>I've build this based on the accepted answer from this question:</p> <p><a href="https://stackoverflow.com/q/499126/1456376">jQuery Set Cursor Position in Text Area</a></p> <h2>Solution 2: Go through all HTML-tags</h2> <p>This is the updated JavaScript to skip through all HTML opening tags. I only posted the updated/new parts.</p> <p><strong>javaScript</strong></p> <pre><code>// new (function ($, undefined) { $.fn.getCursorPosition = function() { var el = $(this).get(0); var pos = 0; if('selectionStart' in el) { pos = el.selectionStart; } else if('selection' in document) { el.focus(); var Sel = document.selection.createRange(); var SelLength = document.selection.createRange().text.length; Sel.moveStart('character', -el.value.length); pos = Sel.text.length - SelLength; } return pos; } })(jQuery); // updated function nextAlt(input) { var current = input.getCursorPosition(); var search = input.val().substr( ( 0 == current ? 0 : current + 1 ) ); var pos = current + search.search(/&lt;[a-zA-Z]+(&gt;|.*?[^?]&gt;)/) + 1; if (-1 == pos) { alert("No elements found"); } else { setCaretToPos(input.get(0), pos); } } </code></pre> <p><strong>Demo</strong></p> <p><a href="http://jsfiddle.net/TmeV3/" rel="nofollow noreferrer">http://jsfiddle.net/TmeV3/</a></p> <p>This one uses a solution from <a href="https://stackoverflow.com/q/1891444/1456376">How can i get cursor position in a textarea?</a> and a regex from <a href="https://stackoverflow.com/q/8038235/1456376">Create a Javascript RegExp to find opening tags in HTML/php template</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload