views:

53

answers:

1

For example, I'm using the following function:

$('body').html( function(i,html) {
    var arr = html.split(". ");
    arr[arr.length-1]="<span class='findme' >"+arr[arr.length-1]+"</span>&nbsp;";
    alert(arr)
    return arr.join(". ");
});

The problem is it moves the cursor away from where the user was typing. Has anyone else seen this?

+1  A: 

By setting the HTML of any element you're essentially resetting all of its content! All event handlers, all states and DOM properties, and this includes any element that currently has 'focus'.

Use the DOM API as it was intended.

It looks like you want to wrap the content following the last period in the page with a <span>. To do this properly you'd need to traverse through the DOM checking for text-nodes that contain the period and then replace part of the last-found text node with a <span> element.

J-P
+1. J-P is absolutely right: you must deal with text nodes rather than HTML strings.
Tim Down
Interesting, can you provide an example of dealing with text nodes?
WozPoz
also, will using text-nodes not effect the Mouse?
WozPoz