tags:

views:

289

answers:

5

Is it possible to move the mouse so that it is positioned inside a text input using JavaScript?

+8  A: 

I don't know about moving the actual rendered mouse, but could you just set the focus to the element?

document.getElementById('the_text_input_id').focus()
jasonbar
impossible to move the mouse, confirmed.
dusoft
+2  A: 

Try

jQuery("#mouse").animate(...)

This should definitely get the pesky rodent where you wanted it to go in the first place.

(John Resig has just commited a patch so that jQuery.animate() now works by bending spacetime instead of moving anything at all.)

Oh, and from now on, this will also work as expected:

jQuery(".dead").animate(...)

If you, however only wanted the caret - or text cursor - instead of the mouse cursor to appear in a text input, it is possible with

jQuery("#textinputid").focus()

To allow your users to avoid messing with the "little beast" at all and type away immediately, overwriting all previous content by their very first keystroke - just like in Excel -, try

jQuery("#textinputid").focus().select()
andras
What? This doesn't make any sense. Please explain how this could possibly work.
Joseph Silvashy
*WHOOOOOSH* That was the sound of andras's humor going over Joseph Silvashy's head.
Jimmy Cuadra
This is **not** a helpful answer.
LiraNuna
+1  A: 

Please see this question:

http://stackoverflow.com/questions/2105733/mouse-move-on-element/2105766

Besides that, I think you are committing major design mistake by taking control of any of the users input in anyway (maybe besides setting the focus of a form element)

Joseph Silvashy
A: 

Here is a function that select text in an input or textarea:

    function textSelect(inp, s, e) {
        e = e || s;
        if (inp.createTextRange) {
            var r = inp.createTextRange();
            r.collapse(true);
            r.moveEnd('character', e);
            r.moveStart('character', s);
            r.select();
        }else if(inp.setSelectionRange) {
            inp.focus();
            inp.setSelectionRange(s, e);
        }
    }

To place the cursor at the 12th position:

textSelect(document.getElementById('theInput'), 12);

To select a portion of the input field:

textSelect(document.getElementById('theInput'), 12, 15);
Mic
sorry for the noise, it's an answer to another question...
Mic
A: 

It would be a huge [security?] issue if they allowed for something like this. Imagine: you have a setInterval(function(){moveMouseToTopLeftCorner and alert garbage}, 1)...
The user would have his mouse moved to the top left. And then alert would show up [which could be closed with enter].. upon which an alert would immediately pop up again.

You'd actually have to use your keyboard to open taskmanager and kill the browser >_>

However, it is probably possible with ActiveX [although thats IE only... and dumb]

ItzWarty