-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
  • 163
  • 1
  • 2
  • 10

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,357
  • 8
  • 29
  • 53
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
  • 24,108
  • 10
  • 56
  • 82