jeni
jeni

Reputation: 99

How do I remove last character from input field on click?

I have a form that when buttons are clicked, it enters a value into an input field. I want to add another button that deletes the last character added. How would I accomplish this using jQuery

Upvotes: 5

Views: 16884

Answers (3)

nathan gonzalez
nathan gonzalez

Reputation: 12017

the simple answer is, if you are using jquery, to do something like this:

//select the button, add a click event
$('#myButtonId').on('click',function () { 
    //get the input's value
    var textVal = $('#myInputId').val();
    //set the input's value
    $('#myInputId').val(textVal.substring(0,textVal.length - 1));
});

Upvotes: 3

austincheney
austincheney

Reputation: 1199

var lastChar = function (x) {
        "use strict";
        var a = document.getElementById(x),
            b = a.value;
        a.value = b.substring(0, b.length - 1);
    };

No jQuery required. The x variable is the id of the input you want to mutilate.

Upvotes: 0

arjuncc
arjuncc

Reputation: 3297

<script>
function addTextTag(txt)
{
document.getElementById("text_tag_input").value+=txt;
}
function removeTextTag()
{
var strng=document.getElementById("text_tag_input").value;
document.getElementById("text_tag_input").value=strng.substring(0,strng.length-1)
}
</script>

<input id="text_tag_input" type="text" name="tags" />

    <div class="tags_select">
    <a href="javascript:addTextTag('1')">1</a>
    <a href="javascript:addTextTag('2')">2</a>
    <a href="javascript:addTextTag('3')">3</a>
    <a href="javascript:removeTextTag()">delete</a>
</div>​

Used a modified version of your code itself try

Upvotes: 9

Related Questions