I am trying to clear a textbox and focus on it after clicking a button in an HTML form using JavaScript. I have searched and I am doing what I am seeing others do. For some reason, it's just not working. Am I missing something?
<button type="button" value="ButtonTwo" onclick="clear();">CLEAR and focus</button>
<script type="text/javascript">
function calculate(){
var price = document.getElementById("Enter_Price").value;
var priceNum = Number(price);
var discount = document.getElementById("Discount").value;
var discountNum = Number(discount);
if(priceNum <= 0){
window.alert("Please enter a price that is greater than 0.");
document.getElementById('Enter_Price').focus();
}
else{
if(discountNum < 0 || discountNum > 100){
window.alert("Please enter a value that is between 0 and 100.");
document.getElementById('Discount').focus();
}
else{
var result = priceNum * (1-(discountNum/100));
document.getElementById("Result").value = "$" + result.toFixed(2);
}
}
}
function clear(){
document.getElementById("Enter_Price").value = "";
document.getElementById('Enter_Price').focus();
}
</script>