1
function getPosition () { if ($.browser.msie) { var textPosition = document.selection.createRange().duplicate(); } else($.browser.mozilla || $.browser.opera || $.browser.safari) { var textPosition = window.getSelection(); } } <div><textarea rows="3" cols="50" name="textarea1" id="textarea1" onChange="javascript:storeCursorPosition();" onClick="javascript:getPosition();" onKeyPress="javascript:getPosition();" onFocus="javascript:getPosition();"></textarea></div> <input type="Button" onclick="javascript:if(textPosition.text == null){alert('No position selected')}else{alert('position selected')};"> 

I have the above code, which is not working in Chrome, Safari etc. I'm getting the "No position selected" alert all the time, except for IE. In IE I get the "position" alert..

Any idea why it's not working except for IE??

2
  • 1
    textPosition has no scope outside of the function. Commented Feb 14, 2013 at 14:00
  • 2
    There is no need to browser sniff! if (window.getSelection) { } else {} Commented Feb 14, 2013 at 14:02

1 Answer 1

2

textPosition has no scope outside of the getPosition() function. Adding it as a variable of window should work, but isn't ideal:

var textPosition = null; function getPosition () { if(!window.getSelection) { textPosition = document.selection.createRange().duplicate(); } else { textPosition = window.getSelection(); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Aaahhh :) but still no working :( I'v created a jsfiddle here: jsfiddle.net/5qtLS and even if I write something in the textarea, in chrome it still alert me with no position selected. When you click in the textarea and click the button it should remain position to textarea and alert with position selected.
Found an error in my own code, so now it works :) thx for helping out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.