I have 6 buttons on the page. On every button click, I do the following:
$('#btn1').click(function(){
$('#txt').val("+");
}
)
$('#btn2').click(function(){
$('#txt').val("-");
}
)
$('#btn3').click(function(){
$('#txt').val("*");
}
)
If you observe, the functionality in each click remains the same, the only difference is a new character is added to the textbox. How can i write this generically?
Firstly, I would recommend delegating your event listener, to reduce code clutter. And you can make use of HTML5 data attributes and
$.datato get the effect you desire:Then in your JS file: