1

I have the following code in which when you enter a number in the input and press the button multiply by 3, it gives the result. And when you press clear, it should clear the result (the empty p tag). I have given the value clear() to the clear button and have also used clear() to call the function in JavaScript, but when I run the code and then try the Clear button, it doesn't work. Here is the code:

function multiplyBy3(){
document.getElementById('result').innerHTML = document.getElementById('xx').value*3;
}
function clear(){
document.getElementById('result').style.display = "none";
}
<label for="number">Enter a number:</label>
<input id="xx" type="number">
<p id="result"></p><br>
<button onclick="multiplyBy3();">Multiply by 3</button>
<button onclick="clear();">Clear</button>



Replaced 'clear' by 'rr':

However, when I replace the word clear by another word, it does work. I replaced clear by rr in the following code and it's working perfectly fine. Here is the modified code:

function multiplyBy3(){
document.getElementById('result').innerHTML = document.getElementById('xx').value*3;
}
function rr(){
document.getElementById('result').style.display = "none";
}
<label for="number">Enter a number:</label>
<input id="xx" type="number">
<p id="result"></p><br>
<button onclick="multiplyBy3();">Multiply by 3</button>
<button onclick="rr();">Clear</button>


I also checked the Reserved words/keywords but it isn't one of them. Can anyone tell me why it isn't working?

0 Answers0