Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm adding another answer here because I believe my original answer really does answer your question as stated (how to get adjacent characters).</p> <p>However after you posted your code I see you want to create a magnifier. That's kinda a different kettle of fish so I rewrote your code.</p> <p>The reason it's different is then you need to use the mouseover to get individual letters and that means you must encapsulate individual letters into tags. The code posted does that (essentially a working version of your code.) Note I didn't mess with your draggable. If you're having issues with that time for a new question.</p> <pre><code>&lt;style type="text/css"&gt; body { font-family:"Tiresias PCfont Z"; /* font-size:15px; */ background: #fff; line-height:1.1em; } .draggable { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } #draggable { margin-bottom:20px; } #MagDrag {background-image:url(controls/MagLens1.png); width: 236px; height: 73px; } #containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px; display:none;} #magnifyView { width: 90px; background-image:url(controls/MagLens1.png);width: 212px;height: 50px; white-space: nowrap; font-size: 40px; font-weight:bold; } .onText { color:#990000;} &lt;/style&gt; &lt;script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://www.java2s.com/Tutorial/JavaScriptDemo/js/ui/ui.core.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://www.java2s.com/Tutorial/JavaScriptDemo/js/ui/ui.draggable.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" charset="utf-8"&gt; //custom "magnify" function var mag = function(){ return { add: function(){ //encapsulate #article into individual spans //why? mouseover events are triggered by tags, not individual letters var str = $('#article').text(); var output = ''; for(var i = 0; i&lt;str.length; i++){ output += '&lt;span&gt;' + str.substr(i, 1) + '&lt;/span&gt;'; } //replace #article with altered html $('#article').html(output); //add the mouseover $('#article span').mouseover(function(){ //note both these selectors could be way more efficient //however this should get you started... var leading = $(this).prev().add($(this).prev().prev()).add($(this).prev().prev().prev()); var trailing = $(this).next().add($(this).next().next()).add($(this).next().next().next()); $('#magnifyView').html( $(leading).text() + '&lt;span class="onText"&gt;' + $(this).text() + '&lt;/span&gt;' + $(trailing).text() ); }); $('#containment-wrapper').show(); }, remove: function(){ $('#article span').unbind('mouseover'); $('#article').html($('#article').text()); $('#containment-wrapper').hide(); } } }(); // jquery $ function load $(document).ready(function(){ $('.addmag').click(function(){ mag.add(); }); $('.removemag').click(function(){ mag.remove(); }); $("#magnifyView").draggable({ containment: '#containment-wrapper', scroll: false, axis: 'x' }); }); &lt;/script&gt; </code></pre> <p>Note, you'll need to click the "addmag" or "removemag" to add or remove the magnifier functionality. (This is there to demonstrate how to encapsulate a string using jquery and a basic loop as well as how to get the original string back, preserving spaces etc.)</p> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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