You have a fundamental misunderstanding as to how this code will be executed. As the page loads, once the browser encounters a <script>
element with code not contained (i.e., within a function), it will execute that script.
So, when your page loads, it looks at your <script>
block and:
- Sets the variable
q
to 13. - Compares
q
to12
using the less than (<
) comparator. - Since the above conditional is false, it will not step into the brackets.
Your button sets q
to 11, but that is irrelevant at that point in the code, because it no longer checks the conditional. Use Javascript functions to achieve the functionality you desire:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function compare(q) {
if(q < 12) {
alert("clear");
}
</script>
<button onclick="compare(11)">clear</button>
</body>
</html>
2
solved What am I doing Wrong? HTML and Javascript [closed]