Can I use a string variable in document.getElementById()?

here is my code:

function figureSelector () {
    document.getElementById("rook").onclick = function  () {
        curr = '"rook"';

function moveLine(){
    document.getElementById("upButton").onclick = function() { 
            document.getElementById(curr) = document.getElementById(curr).offsetTop - getPix() * 62  + "px"; 
            counter= counter + getPix();

I want to write an universal function for a chess piece to move. All I want is, when one clicks the chess piece, and then presses the up button, it must go up.

Yes, you can. Just use

curr = 'rook';

(without the extra quotes)

Yes you can use String variable:


<div id="name" style="width:300px;height:300px;background:red"></div>


var b = 'name';
document.getElementById(b).innerHTML = 'none';

jsfiddle here

You can even do stuff like this:

function mark(yy)  {
    for (var ii = 0; ii < 7; ii++ )  {
        if (  ii == yy )  {
            document.getElementById("b"+ii).style.fontWeight = "bold";

But be careful of your loop limits to be sure you have a matching id. I just spent hours trying to figure out why ("b"+ii) was getting error and finally realized that the error was being caused by exactly that. duh...