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 acplish this using jQuery
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 acplish this using jQuery
Share Improve this question edited Sep 2, 2013 at 15:53 Cody Guldner 2,8961 gold badge26 silver badges36 bronze badges asked Aug 29, 2012 at 21:12 jenijeni 991 gold badge2 silver badges8 bronze badges 7- 5 what have you tried? can you post an simple example of the page on jsfiddle? – nathan gonzalez Commented Aug 29, 2012 at 21:13
- do you mean a new character is attached at the end of your sting on each button click? – arjuncc Commented Aug 29, 2012 at 21:19
- referthis from the stack-overflow itself. You have a good example too. [1]: stackoverflow./questions/952924/… – arjuncc Commented Aug 29, 2012 at 21:23
- so this is basically what I am doing so far.. jsfiddle/jeni/jfs4x – jeni Commented Aug 29, 2012 at 21:34
- Thanks for all the replies, I'm sure they all worked great but arjuncc made it really simple! – jeni Commented Aug 30, 2012 at 0:40
3 Answers
Reset to default 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
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));
});
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.