0

It might be a newbie question so I appologize in advance.

I added an element (header) to a div in javascript and I want to change it's position. For some reason this is not possible.

Below is a code sample.

Any idea what I am missing?

        var header = document.createElement("H7");
        var text = document.createTextNode(cardNum);
        header.appendChild(text);

        header.style.position="relative";
        header.style.left="400";

         //i and j are indexes
        document.getElementsByClassName('masonry-column')[j].getElementsByClassName('subject-details')[i].appendChild(header);

Update: Updated the code and the issue still remains. There are no errors in the console, the styles just do not take affect.

6
  • 1
    Header elements only go up to six (H6). I haven't looked beyond this. Although.. you are attempting to append the element three times. Append it once then change its style (or do this before appending). Commented May 31, 2014 at 22:42
  • Why do you append the same element 3 times? You can just write header.style.position = 'relative' and header.style.left = 400. After that, you just need one line to append if to the header. Commented May 31, 2014 at 22:44
  • But this question lacks: 1) information about what i and j are. 2) relevant error messages. Did you check for script errors? 3) the accompaniing HTML snippet, and preferably a jsfiddle that reproduces the problem 4) a basic lack of understanding about the problem in general (or so it seems). Commented May 31, 2014 at 22:46
  • @GolezTrol: You are correct about appending the element multiple times, I fixed my code but the issue still remains (thanks for the tip though). As for your questions: i, j are just indexes for classes in the DOM (there are multiple masonry-column elements). I do not get any error message, the changes just do not take place. I have to say I am new to JS so I apologize if my question is unclear. Commented May 31, 2014 at 22:52
  • Have you looked in the console in the developer tools (F12)? Javascript errors nowadays don't show in the browser as they used to do in old IE versions. Sorry for asking, I just want to be sure. Commented May 31, 2014 at 22:56

2 Answers 2

2

If you fixed the javascript errors pointed out by others, I suspect the problem isn't the javascript so much as the css.

The css left property requires some form of measurement--px, %, cm, em-- take your pick, but you have to choose one! ;-) Try something like:

header.style.left='400px';
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

var header = document.createElement("H6");
var text = document.createTextNode(cardNum);

header.appendChild(text);

document.getElementsByClassName('masonry-column')[j].getElementsByClassName('subject-details')[i].appendChild(header);
header.style.position='relative';
header.style.left= 100; 

1 Comment

Same issue (the styles did not take effect), thank you for the comment though :)

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.