Skip to content Skip to sidebar Skip to footer

How To Check If Cursor Is Between Specified Tags

There are some formatted text inside the textarea and a cursor is in some place. I need to check if the cursor is between some specific tags. Example: foo|bar. isBetween(['b', 'str

Solution 1:

 You should enclose the space between the tags with a span tag. Then, in jQuery, use the .mouseover() function.

Example:

<b>TAG1</b><spanid="spacer">&nbsp;</span><strong>TAG2</strong><scripttype="text/javascript">
    $("#spacer").mouseover(function(){
        //Do stuff
    });
</script>

The span has to have something in it, so throw an &nbsp; in there as a space.

Live fiddle example: http://jsfiddle.net/PrruT/

UPDATE:

I'm not going to do the whole function for you, but I'll tell you that you could set it up like this:

/* I'm assuming you can figure out how to get the cursor position on your own, represented by var cursor */functionisBetween(selectorOne, selectorTwo){
    var left = $(selectorOne).position().left; //Left position of the objectvar right = $(selectorTwo).position().left + $(selectorTwo).width(); //Right XY position of the objectif (cursor > left && cursor < right)
        returntrue;
    elsereturnfalse;
}

Post a Comment for "How To Check If Cursor Is Between Specified Tags"