0

In my container news-variety I got 4 columns, I just want to take all column in to left and animate each column to write.

For that I use the CSS left property.

I wrote the function to get the no. of column, but it adds whites space and display wrongly, in my case I need the i value 0,1,2,3,4 but this function shows 0, 2, 4, 6, 8.

Please help me to remove the whites space and get the i value to 0,1,2,3,4.

This is my code :

var newsVariety = document.getElementById('news-variety');
if(newsVariety){
    var newsColums = newsVariety.childNodes;
    for(i=0;i<newsColums.length;i++){
        if(newsColums[i].className == "column") continue;
        alert(i);
    }
}
1
  • Your i values appear not to progress linearly because of the continue statement. If newsColumns[i].classname == 'column", then your alert(i) never gets called. Move it up a line and you'll see a linear progression. Ass for the rest, well, I've no idea what you're asking. Commented Feb 26, 2011 at 8:30

2 Answers 2

1

You're getting whitespace (text) nodes in addition to elements. You could exclude the text nodes like this:

var newsVariety = document.getElementById('news-variety');
    if(newsVariety){
        var newsColums = newsVariety.childNodes;
        for(i=0;i<newsColums.length;i++){
            if(newsColums[i].className == "column") continue;
            if(newsColums[i].nodeType == 3) continue;
            alert(i);
        }
    }

but a better solution would be to use a function (getElementsByTagName) that only gives you elements in the first place, like this:

var newsVariety = document.getElementById('news-variety');
    if(newsVariety){
        var newsColums = newsVariety.getElementsByTagName('whatever-your-element-tag-is');
        for(i=0;i<newsColums.length;i++){
            if(newsColums[i].className == "column") continue;
            alert(i);
        }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

i use like this : var newsVariety = document.getElementById('news-variety'); if(newsVariety){ var newsColums = newsVariety.getElementsByTagName('DIV'); for(i=0;i<newsColums.length;i++){ if(newsColums[i].className == "column") continue; newsColums[i].style.left = (i*23) +'%'; } } but not work
if i remove this line "if(newsColums[i].className == "column") continue;" then it's works fine. what is wrong?
How does it not work? Also, are you sure you want to continue if className == "column"? Aren't the divs with "column" class the ones you actually want to change?
The continue statement means "skip all the lines after this one and go through the loop again". So you probably want to remove that "if" and "continue" section.
i want to change the div with colum class. as well column div have the siblings and childrens as divs. if i remove the class name then the animation will affect all divs right?
|
1

You are forgetting that whitespace is represented in the DOM as text elements.

if (newsVariety) {
  var newsColums = newsVariety.childNodes;
    for (col = 0, i = 0; i < newsColums.length; i++) {
      if (newsColums[i].className == "column")
        continue;
        alert (col++);
     }
 }

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.