1

I won't to run this function when user press the backspace (8) or enter(13).

$(document).keypress(function(e) {
    if(e.which == 8) { //-- or 13
        var x = document.getElementsByTagName("BODY")[0];
        x.style.backgroundImage = "-moz-linear-gradient(--90deg, #004158 0%, #005472 100%)";
        x.style.backgroundImage = "-webkit-linear-gradient(--90deg, #004158 0%, #005472 100%)";
        x.style.backgroundImage = "-o-linear-gradient(--90deg, #004158 0%, #005472 100%)";
        x.style.backgroundImage = "linear-gradient(-180deg, #004158 0%, #005472 100%)";
    }
});

But this code doesn't work.

Thanks.

  • 2
    won't or want???? `e.which == 8` is want and `e.which != 8` is won't. – Ajay Narain Mathur Jul 05 '15 at 09:14
  • What's the point of setting the `.backgroundImage` property and then overwriting it three times with other values? I assume you intend to apply all of those gradient settings at once? Probably easier to create a class in your stylesheet and use jQuery to add that class when required. – nnnnnn Jul 05 '15 at 09:19
  • i guess she wants to mimic the CSS pattern, where you use the vendor-specific gradient before the original for browsers that don't support linear-gradient by now. – jhinzmann Jul 05 '15 at 09:24
  • In css the body have already this property. I want to overwriting this property to change the background. –  Jul 05 '15 at 09:28
  • @MicheleCastoldi: check my answer, i think this is what you want. – Ajay Narain Mathur Jul 05 '15 at 09:31

3 Answers3

1

what jQuery version you use?

$(document).on('keypress', function(e) {
if(e.which == 8 || e.which == 13) { //-- or 13
    var x = document.getElementsByTagName("BODY")[0];
    x.style.backgroundImage = "-moz-linear-gradient(--90deg, #004158 0%, #005472 100%)";
    x.style.backgroundImage = "-webkit-linear-gradient(--90deg, #004158 0%, #005472 100%)";
    x.style.backgroundImage = "-o-linear-gradient(--90deg, #004158 0%, #005472 100%)";
    x.style.backgroundImage = "linear-gradient(-180deg, #004158 0%, #005472 100%)";
     e.preventDefault();
}
});

http://jsfiddle.net/pmq0215r/

metamorph_online
  • 204
  • 2
  • 11
0

Try keydown instead of keypress.

$(document).keydown(function(e) {
    if(e.which == 8) { //-- or 13
        alert('8');
    }
});

See this

Community
  • 1
  • 1
Matthew King
  • 1,342
  • 7
  • 13
0

Change --90deg to -90deg , --90deg is not valid value. and you may use jQuery to set background

Run snippet and hit enter on body, jquery v1.2.3 is oldest available on snippet i have used it:

$("body").keypress(function(e) {
  if (e.which == 8 || e.which == 13) {

    $("body").css('background', '-moz-linear-gradient(-90deg, #004158 0%, #005472 100%)');
    $("body").css('background', '-webkit-linear-gradient(-90deg, #004158 0%, #005472 100%)');
    $("body").css('background', '-o-linear-gradient(-90deg, #004158 0%, #005472 100%)');
    $("body").css('background', 'linear-gradient(-90deg, #004158 0%, #005472 100%)');
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
Ajay Narain Mathur
  • 5,326
  • 2
  • 20
  • 32