15

In JQuery 1.6.1, we can supposedly use += or -= with css() just as we can do with animate(), but this isn't working. Does anyone see a problem with the code?

$(document).keydown(function(e) {  
    if (e.which == 37) { //37 left arrow key.
        $('div').css('left', '-=10px')
    }
});

Check http://jsfiddle.net/QLFEy/3

Pinkie
  • 10,126
  • 22
  • 78
  • 124
  • 2
    Moderator note: Comments under this question have been removed due to community flags on comments that would quickly result in a disjointed and confusing conversation once automatically deleted. – Tim Post May 13 '11 at 04:26

2 Answers2

16

Your code is correct and should work. Looking at jQuery bug tracker, i found an already open ticket for this at http://bugs.jquery.com/ticket/9237. It works with width and height but not with left at least. According to the ticket, fix should be available in 1.6.2

Update 05/13/11

The issue has been fixed and the fix is currently available in the WIP version which can be found at http://code.jquery.com/jquery-git.js. This is the very latest non production version of jQuery with most recent fixes. The fix should be be officially available in the upcoming 1.6.2 version.

Hussein
  • 42,480
  • 25
  • 113
  • 143
  • 2
    The bug references hyphenated properties. `left` isn't hyphenated. – gen_Eric May 12 '11 at 19:52
  • @Rocket Please familiarize yourself with jQuery changlog before posting. According to jQuery 1.6 change log, we can use left, http://blog.jquery.com/. – Pinkie May 12 '11 at 19:56
  • @Pinkie: Please familiarize yourself with this question (you asked it). According to the jsFiddle you posted, we can't use left, seems to be a bug. – gen_Eric May 12 '11 at 19:57
1

Obviously a bug in jQuery. It seems like jQuery is not adding "px" after the value.

[2011-05-12 21:46:17] CSS - file://localhost/D:/test.html
DOM style property
Invalid value for property: margin-left
Line 1:
  260
  ---^
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
seler
  • 8,803
  • 4
  • 41
  • 54