$('#donationvalue').on('input change', function() { var vals = [1, 2, 5, 10, 15, 20, 25, 30, 50, 100]; var val = $('#donationvalue').val(); var rval = parseInt(val, 10) + 1; $('#donationoutput').text(vals[val]); $('#donationicons span').removeClass('selected'); $('#donationicons span:nth-of-type(' + rval + ')').addClass('selected'); }); $('#donationicons span').click(function() { var val = $('#donationicons span').index(this) / 2; $('#donationvalue').val(val); $('#donationvalue').change(); });