views:

63

answers:

1

Hi All,

In my web page there is a textbox with width and height equal to 0. This textbox is used to get the scanned bar code value which should not be visible to the user.

Needs:

  1. I want to set focus to the textbox, when i click anywhere in the form.
  2. I want to make the cursor invisible.

Geetha.

+2  A: 

As for setting focus,

$('#idOfTextBox').focus();

will do the trick. You can add that to any click handler, such as

$('#idOfFormOrParentDiv').click(function() {
    $('#idOfTextBox').focus();
    $(this).css('cursor', 'none');
});

As for hiding the cursor, you could apply a cursor: none to the form or parent div as a style or as in the preceding example change it's css via jquery.

The cursor:none is while the cursor is in the bounds of the element - though this behavior in it's entirety seems obscure and unnecessary (not to mention confusing to the user).

Example

Here's an example of using absolute positioning to both hide the input field and its cursor. It focuses the field on load, so if you type, then press the Go button, you'll see what you typed.

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
#myTextBox {
    position: absolute;
    left: -10000px;
    top: 0px;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'&gt;&lt;/script&gt;
<script type='text/javascript'>
(function() {
    $(document).ready(pageInit);

    function pageInit() {
        $('#btnGo').click(go);
        $('#myTextBox').focus();
    }

    function go() {
        alert($('#myTextBox').val());
    }
})();
</script>
</head>
<body><div>
<input id='myTextBox' type='text' size='1'>
<input id='btnGo' type='button' value='Go'>
</div></body>
</html>
Dan Heberden
@Dan: I expect he means the big blinking cursor that shows when the focus is in a text box, not the mouse cursor.
T.J. Crowder
Yes. You are right. I want to hide the blinking cursor inside the textbox.
Geetha
Oh, heh - but if the textarea is 0px x 0px, how would you see it anyway? Otherwise you could have the text area well out of the window viewport (margin-left:1000px; ?) and on it's change event update another textarea that isn't in focus (thus, no blinking cursor) or even a div?
Dan Heberden
@Dan: Oh that's too smart; recommend updating the answer text. I just tried it out of curiousity, and it worked a great on Chrome 4, IE7, and Firefox 3.6. They let me focus the text box, but I didn't see anything on-screen. Just set `position: absolute; left: -10000px; top: 0px` on the `input`. At a stroke, that effectively hides the text box *and* the cursor. (I was a bit surprised that I didn't have to wrap it in a container and set the container's `overflow: hidden` style, but I didn't; I guess because it's off to the left, not the right.)
T.J. Crowder
@Dan: Since I'd done it, I took the liberty of posting in my test/demo page.
T.J. Crowder
Ah, the power of collaboration! Where's that test/demo page? You cool if we put that in the answer?
Dan Heberden
My bad, I was editing it when you did and overwrote it - i rolled back the edit :)
Dan Heberden