0
function moveDoi(n) {
        var delay = -1;
        var left = document.getElementById("One");         
        var currentLeft = parseInt(getComputedStyle(left).left, 10);
        setTimeout(move, delay);
        function move(){
          if (currentLeft <= n ) {            
            currentLeft+=3;            
            left.style.left = currentLeft + "px";            
            setTimeout(move, delay);       
          }
        }
      };

In this code I have n as a parameter of the function, that is used in the if conditional statement. For this function i need to have one more parameter x that will change the "One" so i need to obtain something like this: document.getElementById(""+x+"") . However, this is not working?

How can I add one more parameter to the function that needs to be in quotes (" ") in function?

1
  • since your x will be a string, you need not put it inside another set of quotes. Simply use document.getElementbyId(x). Commented Jan 8, 2014 at 15:49

1 Answer 1

1

First, you need to pass x into the function. Then, you simply need to reference it when calling document.getElementById. There are is no need to append/prepend quotes, since x is already a string.

function moveDoi(n, x) {
        var delay = -1;
        var left = document.getElementById(x);         
        var currentLeft = parseInt(getComputedStyle(left).left, 10);
        setTimeout(move, delay);
        function move(){
          if (currentLeft <= n ) {            
            currentLeft+=3;            
            left.style.left = currentLeft + "px";            
            setTimeout(move, delay);       
          }
        }
      };
Sign up to request clarification or add additional context in comments.

2 Comments

Can you add comments to explain what the OP was doing wrong, what should he have done instead?
much better. Adding comments helps them not make the same mistake again.

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.