0

I have this:

<div class="graph">
  <rect y="2%" class="rect"></rect>
  <text>1</text>
  <rect y="15%" class="rect"></rect>
  <text>2</text>
  <rect y="11%" class="rect"></rect>
  <text>3</text>
</div>

And an array like this: var labels = ["one", "two", "three"];

And I do this:

labels.forEach(function (label, index) {
        if (index == 0) {
           // get first rect and text tag
        } else if (index == 1) {
           // get second rect and text tag
        } else {
           // get third rect and text tag
        }

    })

I would like to get in first iteration the first "rect" HTML tag and the first "text" HTML tag; in the second iteration the second and in the third iteration the third.

1
  • Have you tried document.getElementsByTagName("rect")[index] ? Commented Jan 4, 2016 at 12:28

6 Answers 6

4

You can get your rects by querySelectorAll which is better supported than getElementsByClassName

  • tag document.querySelectorAll("rect") or
  • class document.querySelectorAll(".rect")

and combine with a normal for loop since forEach does not work on nodelists - read more here.

var labels = ["one", "two", "three"];
window.onload=function() {
  var rects = document.querySelectorAll("rect");
/*  
  rects.forEach(function(rect,index) { // fails
    rects.appendChild(document.createTextNode(labels[index]));
  });
*/  
  for (var i=0;i<rects.length;i++) {
    rects[i].appendChild(document.createTextNode(labels[i]));
  };
  
}
<div class="graph">
  <rect y="2%" class="rect"></rect>
  <text>1</text>
  <rect y="15%" class="rect"></rect>
  <text>2</text>
  <rect y="11%" class="rect"></rect>
  <text>3</text>
</div>

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

1 Comment

Thanks!! I will try it!
0

Hi you can try this code in your html page:

<div class="graph" >
  <rect y="2%" class="rect"></rect>
  <text id="one">1</text>
  <rect y="15%"  class="rect"></rect>
  <text id="two">2</text>
  <rect y="11%"  class="rect"></rect>
  <text id="three">3</text>
</div>
<script type="text/javascript">
var labels = ["one", "two", "three"];
labels.forEach(function (label, index) {

        if (index == 0) {
            document.getElementById("one").innerHTML = "TEST WRITE";
           // get first rect and text tag
        } else if (index == 1) {
           // get second rect and text tag
        } else {
           // get third rect and text tag
        }

    })
</script>

Put your Javascript code after HTML.

Comments

0
<div class="graph">

</div>
<script>
var string = "";
var labels = ["one", "two", "three"];
     labels.forEach(function (label, index) {
        if (index == 0) {
           // get first rect and text tag
           string = string + '<rect y="2%" class="rect"></rect>'+
                    '<text>1</text>';
        } else if (index == 1) {
           string = string + '<rect y="15%" class="rect"></rect>'+
                    '<text>2</text>';
        } else {
           string = string + '<rect y="11%" class="rect"></rect>'+
                    '<text>3</text>';
        }

    });
$(".graph").append(string);
</script>

1 Comment

It would be helpful to add some text to this, so you explain what you have done.
0

You can get all rect by class.

var elements = document.getElementsByClassName("rect");

You can iterate like you did and use the index to match

var labels = ["one", "two", "three"];
var elements = document.getElementsByClassName("rect");

labels.forEach(function (label, index) {
    alert(label + ": " +  elements[index].getAttribute("y"));
});

You can see it at: https://jsfiddle.net/7Lecaezy/

Comments

0

You can get all rects by className and then target the text elements relatively:

var rects = document.getElementsByClassName('rect');
var curRect, curIndex;
for(var i=0; i<rects.length; i++){

    curRect = rects[i];
    curIndex = curRect.nextSibling.innerHTML;

    switch(curIndex){
        case "0":
            //do something
            break;
        case "1":
            //do something
            break;
        case "2":
            //do something
            break;
        default:
            //default behavior
    }
}

Keep in mind that this is all relative, in other words it may break if the structure changes. It might be better to assign something with which you could select them directly.. For example if you had the following structure:

<div class="graph">
   <rect y="2%" class="rect"></rect>
   <text id="rect-text-1">1</text>
   <rect y="15%" class="rect"></rect>
   <text id="rect-text-2">2</text>
   <rect y="11%" class="rect"></rect>
   <text id="rect-text-3">3</text>
</div>

You could then do

for(var i=0; i<rects.length; i++){
    curIndex = document.getElementById('rect-text-' + i).innerHTML;
}

Targeting by ID is much more reliable than relative, because it's structure independent.

Comments

-1

I would also recommend jQuery here. Specifically nth-child.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.