-3

I can't find why it is creating this error for the life of me. I am trying to change the position of "snake" with css bottom and left in javascript. Here is my code:

https://jsfiddle.net/9zewsf3x/2/

This is the what is causing the errors apparently.

snakeObject.style.bottom = x_pos + "px";
snakeObject.style.left = y_pos + "px";
JudasMoses
  • 378
  • 4
  • 22
  • 2
    Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Andreas Jul 07 '17 at 08:17
  • Moreover, I have no error in my console when I test your fiddle. – sjahan Jul 07 '17 at 08:18
  • Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Andreas Jul 07 '17 at 08:19
  • 2
    When do you load the script? Is it possible that at the point when you load the script the snake id doesn't exist? – Spitzbueb Jul 07 '17 at 08:19
  • No error in my console (tested with FF54 and Chrome59). No definition of expected behavior. Incomplete code (e.g. `snakeY.style.bottom = y + 1 + "vh";` but no declaration of `snakeY` anywhere). – Seika85 Jul 07 '17 at 08:57

2 Answers2

0

just use style.cssText

snakeObject.style.cssText = "left: 50px; right: 50px;"
susan
  • 13
  • 3
-3

You were close to what you wanted to achieve

https://jsfiddle.net/tpc8ukgj/

dummy code block for stackoverflow

But the question itself was wrong, the error you postet did not occur!

edit:

here is the relevant part, that was missing in your script:

snakeObject.style.bottom = (parseInt(snakeObject.style.bottom) - 1) + "px";

parseInt will remove the "px" from the value you get from style, so you can add another pixel. After that you simply add "px" again and the integer will be caseted to a string that is concatenated to the value, that you need.

Philipp Wrann
  • 1,751
  • 3
  • 19
  • 29
  • 1
    Please [avoid link only answers](http://meta.stackoverflow.com/tags/link-only-answers/info). Answers that are "barely more than a link to an external site” [may be deleted](http://stackoverflow.com/help/deleted-answers). (The fact you need a "dummy code block" should have been a clue that you were doing something wrong!!) – Quentin Jul 07 '17 at 08:38
  • If you don't have enough reputation to comment then don't comment. Answering a question with a non-answer is not the way. – Quentin Jul 07 '17 at 08:42
  • I don't understand why you people are so angry. This answer is very helpful. – JudasMoses Jul 07 '17 at 08:46
  • I do understand it is not optimal to link an external source as answer - the link may break and the answer is worthless. I will try to avoid that in future. Glad i could help you anyway :) – Philipp Wrann Jul 07 '17 at 09:44