最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Deselect text with Javascript - Stack Overflow

programmeradmin18浏览0评论

I have two input tag with numeric id in a form in one html page. On focus, I would like to set value of the second input from first and second character of the first input. Then I want to deselect the selected text in second input. There is a way to do that? I wrote this HTML code:

<label>Num. Fattura:
        <input type="text" name="numfattura1" onkeypress="return event.keyCode != 13" id="1" />
    </label>
    <label>Importo:
        <input type="text" name="importo1" onkeypress="return event.keyCode != 13" />
    </label>
    <br />
    <label>Num. Fattura:
        <input type="text" name="numfattura2" onkeypress="return event.keyCode != 13" id="2" onfocus="twoDigits(2)" />
    </label>
    <label>Importo:
        <input type="text" name="importo2" onkeypress="return event.keyCode != 13" />
    </label>
    <br />

And this javascript function:

function twoDigits(id){
    var previousId = id - 1;
    var prevoiusValue = document.getElementById(previousId).value;
    document.getElementById(id).value = prevoiusValue.substring(0,2);
    document.selection.empty();
    window.getSelection().removeAllRanges()
}

I can't find how to deselect the text that is selected automatically.

I have two input tag with numeric id in a form in one html page. On focus, I would like to set value of the second input from first and second character of the first input. Then I want to deselect the selected text in second input. There is a way to do that? I wrote this HTML code:

<label>Num. Fattura:
        <input type="text" name="numfattura1" onkeypress="return event.keyCode != 13" id="1" />
    </label>
    <label>Importo:
        <input type="text" name="importo1" onkeypress="return event.keyCode != 13" />
    </label>
    <br />
    <label>Num. Fattura:
        <input type="text" name="numfattura2" onkeypress="return event.keyCode != 13" id="2" onfocus="twoDigits(2)" />
    </label>
    <label>Importo:
        <input type="text" name="importo2" onkeypress="return event.keyCode != 13" />
    </label>
    <br />

And this javascript function:

function twoDigits(id){
    var previousId = id - 1;
    var prevoiusValue = document.getElementById(previousId).value;
    document.getElementById(id).value = prevoiusValue.substring(0,2);
    document.selection.empty();
    window.getSelection().removeAllRanges()
}

I can't find how to deselect the text that is selected automatically.

Share Improve this question edited Nov 9, 2011 at 19:07 Lightness Races in Orbit 386k77 gold badges666 silver badges1.1k bronze badges asked Nov 9, 2011 at 18:50 Andrea VenanziAndrea Venanzi 2505 silver badges15 bronze badges 2
  • stackoverflow./questions/794583/… – Birey Commented Nov 9, 2011 at 19:00
  • Thanks but I'm not using JQuery :D – Andrea Venanzi Commented Nov 9, 2011 at 19:46
Add a ment  | 

3 Answers 3

Reset to default 2

set selectionStart and selectionEnd using setTimeOut.

http://jsfiddle/enQvT/ works in firefox.

function twoDigits(id){
    var previousId = id - 1;
    var prevoiusValue = document.getElementById(previousId).value;

    var elem = document.getElementById(id);
    setTimeout(function() {
               elem.selectionStart=0;
                elem.selectionEnd=0;
                }, 0);
    elem.value = prevoiusValue.substring(0,2);
}

Call .focus() on a another form element.

You can also empty and refill the input. If you want to test:

<input type="textfield" id="aaa" value="aaa" />
<input type="textfield" id="bbb" value="bbb" onmouseover="empty()"  />

<script>
var empty = function() {
    var ipt = document.getElementById('aaa');
    var iptval = ipt.value;
    ipt.value = "";
    ipt.value = iptval;
}
</script>
发布评论

评论列表(0)

  1. 暂无评论