show cursor/caret at the beginning of input box

Zj Wine

PROBLEM: I have a html text input box with some text in it, when user click the input box, I would like to have the caret/cursor appear at the beginning of the input box.

I could move a caret by using setSelectionRange() function, but I don't like the effect that the caret shown at the end of the text input box then moved to the beginning. I would like it to be at the beginning of the input upon shown.

example: http://jsfiddle.net/edh8mkht/1/

HTML

<body>
    <input type="text" id="myP" onmousedown="mouseDown()" value="Mozilla" />
</body>

JS

function mouseDown() {
    document.getElementById("myP").style.color = "green";
    document.getElementById("myP").setSelectionRange(0,0);
}

QUESTION 1:

I use mousedown event to move the caret, but it doesn't move the caret at all, the same thing happens if I use onfocus event. Is that because the caret appears after this two events and making setSelectionRange has no effect?

QUESTION 2:

What's a good way to solve my problem by using javascript? Note: I cannot use placeholder.

Josh Burgess

Fiddle showing initial selection of text box

Requirements for this to work:

  1. Have to be able to wrap your input in a label (or really any element which can accept a child node)
  2. Have to support pointer-events CSS property
  3. You have to be able to move the firing event from the input to the label

HTML

<label for="myP" onclick="click();">
    <input type="text" id="myP" value="Mozilla" />
</label>

CSS

input#myP {
  pointer-events: none;
}

JavaScript

function click() {
    var input = document.getElementById("myP");
    input.style.pointerEvents = 'auto';
    input.style.color = "green";
    input.setSelectionRange(0,0);
    console.log('click');
}

Important things to consider in this implementation:

  • It's essential that you protect your CSS selector with the element type. In this case, it's input#myP as opposed to #myP. Otherwise, the CSS will select both the label and the input in some browsers if the for attribute is specified.
  • You have to apply some style element to your label, otherwise this simply won't work.
  • Your caret point will always be before the initial value when clicked. At least in Chrome. That's part of the problem with your setSelectionRange call. I'd have to do some research to figure out why that is.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

leaflet geocoding with input box

분류에서Dev

Message box show output of command

분류에서Dev

Autocomplete search box input field

분류에서Dev

regex for text box to validate input

분류에서Dev

How do I show hidden files beginning with a certain letter in Linux?

분류에서Dev

Disable microsoft word "Show Repairs" dialog box

분류에서Dev

Show Windows Search Box on all screens

분류에서Dev

sending input from text box to controller

분류에서Dev

How to resize input box in UIAlertViewStyleLoginAndPasswordInput of Objective C

분류에서Dev

can not add text box input in table

분류에서Dev

Ajax Return Value show on div but not show on input fields

분류에서Dev

.show () 뒤에 <input> 숨기기

분류에서Dev

jQuery: show selected text in the alert box upon Ctrl+a

분류에서Dev

How to show a text box if value in tablix is empty in RDLC report

분류에서Dev

How to show my customaized alert box over bootstrap3 modal box?

분류에서Dev

How to make form input box larger in HTML (along with text)?

분류에서Dev

How to color an input box without generating an inset effect

분류에서Dev

Android - check an EditText input box each type the user types in a character

분류에서Dev

Validation for input box, use Jquery focusin function doesn't work

분류에서Dev

Laravel Ajax search gives the whole collection as response to the query after input is cleared from input box

분류에서Dev

Bootstrap Input Password Show Hide with bootstrap-float-label css

분류에서Dev

How to force an input html element to show only the last inputted character

분류에서Dev

add a show password button inside of input field - crispy forms

분류에서Dev

VBA code to show message box popup if the formula in the target column exceeds a certain value

분류에서Dev

Add an object to WPF Combo box Items collection, but only show specific property to the client

분류에서Dev

jQuery textbox velue multiply and show on another text box on Radio button checked

분류에서Dev

How to load a new page with AngularJS when a user start typing in an input box

분류에서Dev

How can I input data to excel from inputting data in text box where the text box are from two or more Userform Using Excel VB?

분류에서Dev

Adding characters to the beginning of an integer

Related 관련 기사

  1. 1

    leaflet geocoding with input box

  2. 2

    Message box show output of command

  3. 3

    Autocomplete search box input field

  4. 4

    regex for text box to validate input

  5. 5

    How do I show hidden files beginning with a certain letter in Linux?

  6. 6

    Disable microsoft word "Show Repairs" dialog box

  7. 7

    Show Windows Search Box on all screens

  8. 8

    sending input from text box to controller

  9. 9

    How to resize input box in UIAlertViewStyleLoginAndPasswordInput of Objective C

  10. 10

    can not add text box input in table

  11. 11

    Ajax Return Value show on div but not show on input fields

  12. 12

    .show () 뒤에 <input> 숨기기

  13. 13

    jQuery: show selected text in the alert box upon Ctrl+a

  14. 14

    How to show a text box if value in tablix is empty in RDLC report

  15. 15

    How to show my customaized alert box over bootstrap3 modal box?

  16. 16

    How to make form input box larger in HTML (along with text)?

  17. 17

    How to color an input box without generating an inset effect

  18. 18

    Android - check an EditText input box each type the user types in a character

  19. 19

    Validation for input box, use Jquery focusin function doesn't work

  20. 20

    Laravel Ajax search gives the whole collection as response to the query after input is cleared from input box

  21. 21

    Bootstrap Input Password Show Hide with bootstrap-float-label css

  22. 22

    How to force an input html element to show only the last inputted character

  23. 23

    add a show password button inside of input field - crispy forms

  24. 24

    VBA code to show message box popup if the formula in the target column exceeds a certain value

  25. 25

    Add an object to WPF Combo box Items collection, but only show specific property to the client

  26. 26

    jQuery textbox velue multiply and show on another text box on Radio button checked

  27. 27

    How to load a new page with AngularJS when a user start typing in an input box

  28. 28

    How can I input data to excel from inputting data in text box where the text box are from two or more Userform Using Excel VB?

  29. 29

    Adding characters to the beginning of an integer

뜨겁다태그

보관