0
<div id="test"></div>
<script type="text/javascript">

window.names=["heihachi", "forest law"];
window.values=[22, 31];
len=names.length
for (var i = 0; i < len; i++)
{
    document.getElementById('test').innerHTML+= names[i];
    //names[i].css=margin-left: values[i]px;//

}

</script>

This is what I would like to do. The for loop works exactly as intended except for that 10th line. I want each element added to that innerHTML to be moved over a different amount. So heihachi is moved over 22 px, forest law is moved over 31 px, ect. for all other names and values added. What I have put there is my attempt (pseudo-code) but it doesn't work. How can I do this?

2 Answers 2

1

I'd suggest the following:

// declare variables in the local scope:
var names=["heihachi", "forest law"],
    values=[22, 31],
// get a reference to the element outside of the loop (once, versus multiple times):
    test = document.getElementById('test'),
// create an element in which to wrap the text:
    span = document.createElement('span'),
// declare a variable which will become a reference to the node on which we're working:
    _tmp;

// to move the element/words around we need to have 'position: relative':
span.style.position = 'relative';

// iterate over the names array:
for (var i = 0, len = names.length; i < len; i++)
{
    // working on a clone of the 'span':
    _tmp = span.cloneNode();
    // appending a child textNode to that node:
    _tmp.appendChild(document.createTextNode(names[i]));
    // setting the left property of the Node's style object:
    _tmp.style.left = values[i] + 'px';
    // appending the node to the 'test' node:
    test.appendChild(_tmp);    
}

JS Fiddle demo.

I would, however, prefer to have an explicit correlation between the two arrays, in this case converting the arrays into an array of objects (each object having a name and value property):

var namesAndPositions = [{
    'name' : 'heihachi',
    'value' : 22
},{
    'name' : 'forest law',
    'value' : 31
}],
    test = document.getElementById('test'),
    span = document.createElement('span'),
    _tmp;

span.style.position = 'relative';

for (var i = 0, len = namesAndPositions.length; i < len; i++)
{
    _tmp = span.cloneNode();
    _tmp.appendChild(document.createTextNode(namesAndPositions[i].name));
    _tmp.style.left = namesAndPositions[i].value + 'px';
    test.appendChild(_tmp);
}

JS Fiddle demo.

If the goal is to have that measurement (22px and 31px to the left of each element), then you could instead use display: inline-block and set the marginLeft property of the HTMLElement-node:

// everything above this point in the code remains the same

span.style.display = 'inline-block';

for (var i = 0, len = namesAndPositions.length; i < len; i++)
{
    _tmp = span.cloneNode();
    _tmp.appendChild(document.createTextNode(namesAndPositions[i].name));
    _tmp.style.marginLeft = namesAndPositions[i].value + 'px';
    test.appendChild(_tmp);
}

JS Fiddle demo.

References:

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

Comments

0

and here's a more simpler example:

window.names=["heihachi", "forest law"];
window.values=[22, 31];
var len=names.length;

var test = document.getElementById('test');
for (var i = 0; i < len; i++)
{
    var elm = document.createElement("span")
    elm.innerHTML = names[i];
    elm.style.marginLeft = values[i] + 'px';
    test.appendChild(elm);
}

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.