javascript - Append Text to Textbox on Click - Stack Overflow

I currently have a selection of buttons and on click of a button I want to add the text from the button

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());
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341219a4623335.html

相关推荐

  • javascript - Append Text to Textbox on Click - Stack Overflow

    I currently have a selection of buttons and on click of a button I want to add the text from the button

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信