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

javascript - Append Text to Textbox on Click - Stack Overflow

programmeradmin1浏览0评论

I currently have a selection of buttons and on click of a button I want to add the text from the button into a text-box. Every time I click on the button I want to be able to append on to whatever I have in the input field.

What I currently have

$('#js-AddFilterOpenBracket').click(function () {
    $('#js-FilterString').val($(this).text());
});

What I'm Aiming for

$('#js-AddFilterOpenBracket').click(function () {
    $(this).text().appendTo($('#js-FilterString'));
});

I currently have a selection of buttons and on click of a button I want to add the text from the button into a text-box. Every time I click on the button I want to be able to append on to whatever I have in the input field.

What I currently have

$('#js-AddFilterOpenBracket').click(function () {
    $('#js-FilterString').val($(this).text());
});

What I'm Aiming for

$('#js-AddFilterOpenBracket').click(function () {
    $(this).text().appendTo($('#js-FilterString'));
});
Share Improve this question asked Jun 1, 2015 at 16:04 John CoolingJohn Cooling 4154 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

No need to use appendTo as it should be used for elements. Rather, manually append to the current value then set it

$('#js-AddFilterOpenBracket').click(function () {
    var currentVal = $('#js-FilterString').val();
    var newVal = currentVal + $(this).text();
    $('#js-FilterString').val(newVal);
});

May not be the bast way but you could do this

$('#js-AddFilterOpenBracket').click(function () {
    $('#js-FilterString').val($('#js-FilterString').val() + $(this).text());
});
发布评论

评论列表(0)

  1. 暂无评论