I am learning the basics of jQuery and was testing out what I knew, I tried adding 1 to the text of a DOM element (<span>
) that stores a number. The number in the <span>
is increased by 1 successfully, however I feel that the way I am updating the element isn't the best way as it seems that I need to call the same jQuery function twice (see code below).
Would anyone be able to suggest a better practice?
HTML
<button onclick="foo('a')">
<span id="a">0</span> liked this
</button>
JS(jQuery)
function foo(elementId){
$('#' + elementId).text(parseInt($('#' + elementId).text()) + 1);
}
Thank you :-)