1

in the console its giving me the error 'position not defined' using the the code below.. any ideas?

var x = 1;
var y = 1;

function getyx(y, x) {
	var yx = 'Y' + y + 'X' + x ;
	return yx;
}

function getPosition() {
  var position = document.getElementById(getyx(y,x));
	return position;
}

function moveSprite() {
	position.appendChild(img); //error occurs here
}

Maybe get element by Id cant accept a function ? I know this may be a silly question but forgive me :)

2
  • 2
    I think what you need is GetPosition().appendChild(img); Commented Jun 9, 2018 at 20:58
  • 2
    img is not defined anywhere in your provided code. Commented Jun 9, 2018 at 20:58

2 Answers 2

1

It looks like getPosition is the function that selects the element you want, so try calling that function:

function moveSprite() {
  const position = getPosition();
  position.appendChild(img);
}

There's absolutely nothing wrong with passing a string created by a function to getElementById

Sign up to request clarification or add additional context in comments.

1 Comment

I've been stuck on this problem for about 3 hours now. You my friend are the man (or woman) ! Thank again
0

The reason it is saying position can be a few problems, the simplest being you don't have "Y1X1" as an HTML ID.

Seen as your function returns a string it would be able to use getElementById(). If your not sure about the scope, you can go to the top of your code and use var position; however it's most likely not a scope issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.