I want to move a JavaScript function up to the <script> tag from a input tag and it’s not working.
This works:
<input type="text" name="aaa" id="aaa" onkeyup="javascript:this.value=
this.value.substring(0,1).toUpperCase()+
this.value.substring(1,this.value.length);
if (this.value=='')
document.getElementById('aaaLabel').innerHTML='AAA';"
/>
This doesn’t:
<script type="text/javascript">
function FieldOnKeyUp() {
this.value=this.value.substring(0,1).toUpperCase()+
this.value.substring(1,this.value.length);
if (this.value=='')
document.getElementById('aaaLabel').innerHTML='AAA';
}
</script>
<input type="text" name="aaa" id="aaa" onkeyup="FieldOnKeyUp()">
What’s the difference?
The value of
thisis not passed to your separate function. In fact,thisin your function is set to thewindowobject. You need to change to this type of code in order to pass the right value to your function:and your code to this:
Here’s a sample that shows this code working: http://jsfiddle.net/jfriend00/2dJ6x/.