The alternative solution I know nay is only with HTML, she also uses the Javascript.
Unlike the other solutions in it I manipulate the cursor without overriding the value, using the setSelectionRange():
The métodoHTMLInputElement.setSelectionRange()
defines the positions
initial and final selection of the current text in an element <input>
.
Example 1:
<form>
<div>
<input type="text" name="p" value="teste"
onfocus="this.selectionStart = this.selectionEnd = this.value.length;"
autofocus="true"/>
</div>
</form>
Example 2:
var campo = document.getElementById("p");
var tamanho = campo.value.length;
campo.setSelectionRange(tamanho, tamanho);
<form>
<div>
<input type="text" name="p" id="p" value="teste" autofocus="true"/>
</div>
</form>
Obs.:
Considering the possible amount of digits in a <input />
avoid attributing to selectionEnd
a constant value unless you know the maxlength
.
Would it really have to be only using HTML? With Javascript/jQuery it would be very simple to do this.
– Leandro
@Leandro would like that yes. The closest was
onfocus="this.selectionStart = this.selectionEnd = 500;"
or the shape of Diego Vieira, but I thought there was a pattern.– rbz
This "close" using
onfocus=
is actually with Javascript, just like all other responses. Personally I don’t think it’s possible to do it only with html.– Isac
@Isac Yes, so I haven’t closed the question yet, I’m hoping :]
– rbz