5

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

Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
jeni
  • 99
  • 1
  • 2
  • 8

3 Answers3

9
<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

arjuncc
  • 3,227
  • 5
  • 42
  • 77
3

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));
});
nathan gonzalez
  • 11,817
  • 4
  • 41
  • 57
0
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.

austincheney
  • 1,189
  • 9
  • 11