I am trying to update the value of a button with id 'cost' on click. Here is my script :
<script>
var costObj = document.getElementById('cost');
function change() {
costObj.value = 100;
}
</script>
and here is the input html.
<input type = number id = 'cost' onclick = 'change()'>
This does not work saying that costObj in the function change is null, but putting the costObj declaration within the function seems to work. Why is this so ? I thought the function will look for the declaration outside the function if there is no declaration as is the case for var that has primitive type. This kind of shakes my conceptual understanding. Thank you in advance !