views:

57

answers:

2

I've built a small jQuery script that adds a class to witch ever input field I'm writing in. However it only works on all input fields after something is written in the first input field, see dev.resihop.nu for example.

The code:

$('.field').keydown(function () {
  if ($('.field').val() !== 'Ort, gata eller kommun') {  
    $(this).addClass("focus");
  };
});
+7  A: 

It's because you're grabbing the elements one more time with $('.field').val(). Change it to:

$('.field').keydown(function () {
  if ($(this).val() !== 'Ort, gata eller kommun') {  
    $(this).addClass("focus");
  };
});
Gert G
Thanks a bunch!
Kristoffer Nolgren
No problem. I'm glad it helped you.
Gert G
+3  A: 

$('.field').val() returns the value of the first field that matches .field. You have to use $(this).val() to get the value of the element that actually fired the event.

piquadrat