I'm reading over answers here, and they all seem to follow a similar theme that I can't understand, and I don't know what I'm looking at enough to try and google it.
They all have this pattern of onkeypress
return somefunction
, and somefunction
often returns a boolean.
I don't understand which part of this is doing the actual restriction. The return value? The web browser? The HTML?
And how?
None of this makes any sense. I'm floundering quite a bit here with my understanding of javascript. I can't get a handle on what is doing what. Why does returning false
or true
do anything at all?
If anyone can point me in the right direction, I'd very much appreciate it. A lot of resources I've found are either what an array is and how to declare variables, or they skip straight to a solution. I'm just trying to figure out what is happening in those solutions.
Here is the pattern:
<input type="text" onkeypress="return somefunction()">
<script>
function somefunction()
{
return false;
}
</script>
<input type="text" onkeypress="return somefunction2()">
and the js
function somefunction2()
{
return false;
}
Secondary, why does this work in jsfiddle when it's in a script tag, but not with an attached jsfile?