I want to hide and show a button on a particular condition in JavaScript.
Code:
$(function(){
// hide the div initially
$("#a").hide();
// Check to see if there is 250 in the url
var bal_amount = document.getElementById('balance_amount');
if (bal_amount.value > 0) {
$("#a").show();
}
});
HTML
<form>
<input type="text" id="balance_amount">
</form>
<image src="image.jpg" id="a">
But it doesn’t work.
You will need to change this portion of your code –
You are executing the above piece of code inside
document readyevent, which means it will be executed right when the page loads, and only once.To remedy this, you need to place this code inside an event handler –
In this way, whenever the value of the
balance_amountfield changes, this event will trigger and validate your balance amount for your.Here you will find a working demo.
You can further improve your code by checking for invalid input in the text box –