-1

Title may not be correct but i didnt know how to ask my question ! I have encountered a problem with this keyword . While using it as in this code <input type="text" onkeyup="this.value=this.value.toUpperCase();"></input> it works perfectly. But when i allot a function on input element that uses this keyword , it dont work as in
HTML

<input type="text" ></input>

Javascript

var inp=document.getElementsByTagName('input')[0];
inp.onkeyup=up;
function up()
{   
    this.value=this.value.toUpperCase();
}

3 Answers3

0

Can you bind onkeyup event in HTML? If yes then use this code:

<script>
function up(element) {   
    element.value = element.value.toUpperCase();
}
</script>
<input type="text" onkeyup="up(this)"></input>
Keammoort
  • 3,035
  • 15
  • 20
0

How about taking it off of the global scope? Try binding in an IIFE:

    (function bindEventHandler(tag) {

        var inp = document.getElementsByTagName(tag)[0];

        inp.onkeyup = function up() {   
            this.value=this.value.toUpperCase();
        };
    }('input'));
Nevy
  • 31
  • 5
0

If you do it this way, make sure to add the script at the end of your body tags, and then it'll work.

Alexandru Pufan
  • 1,564
  • 2
  • 21
  • 38