It is not wrong, per se, but yes, it is seen as bad practice by the vast majority of people.
Good practice is to separate the HTML and Javascript as much as you can. It is the same concept as with CSS. Mixing up the HTML, CSS and Javascript in the same file can become unmanageable very quickly, and should be avoided, if at all possible.
Ideally, the two should be in separate files (.html
and .js
files), but separating them in the HTML is a good first step.
You can separate the two by using event listeners, like so:
<!-- The elements -->
<span id="valBox">25</span>
<input id="inputBox" type="range" min="5" max="50" step="1" value="25">
<script>
// Grab the elements here
var val = document.getElementById("valBox");
var inp = document.getElementById("inputBox");
// Add an event listener to the input element and
// set the span value when it changes
inp.addEventListener("change", function () {
val.textContent = this.value;
});
</script>
See this answer if you need to support IE<=9.