-1

I found this code from w3schools. There it works but when I run it in jsfiddle there is an issue.

<input type="text" onfocus="myFunction(this)">


function myFunction(x) {
    x.style.background = "yellow";
}

Here is the code in jsfiddle:

sotirios
  • 165
  • 1
  • 2
  • 17

2 Answers2

3
  • you dont need to include jquery.
  • Add your script in body not onLoad.

See the updated fiddle

Edited:Better if it would be added in head

ozil
  • 6,930
  • 9
  • 33
  • 56
0

Using addEventListener works for me.

var input = document.getElementsByTagName("input")[0];

input.addEventListener("focus", function() {
    this.style.background = "yellow";
});

http://jsfiddle.net/f11ppv4L/4/

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87