javascript - d3 change class of clicked node text -


i'm trying add class label, clicked in force diagram. use "click" function, hands clicked item update function (it updates class of item). tried using this console report "nodeclicked.childnodes[1].classed not function".

then googled , tried use "d3.select(this).select("text");" not report error, nor update text.

a node < g > element has 2 children: circle , text (the text want give css class)

if want to, can @ code snippet:

// toggle children on click. // save information clicked , update gui function click(d) {     if (d.children) {         d._children = d.children;         d.children = null;     }     else{         d.children = d._children;         d._children = null;     }     // save node clicked     nodeclicked = d3.select(this).select("text");     // save d3 data of node     nodeclickeddata = d; } 

to change class tested both:

$(nodeclicked).addclass("nodegreenmarked"); 

and:

nodeclicked.classed("nodebluemarked", true); 

but none did anything. if check in console content nodeclicked is, tells me "array[1]" , when open it: "0:< text >"

you can add class in 2 ways.

d3.select(this).select("text").attr("class",classname); 

or

d3.select(this).select("text").classed(classname,true); 

working snippet:

d3.selectall(".node").on("click", function() {    //check if node selected    var text = d3.select(this).select("text");    if (text.classed("selectedtext")) {      text.classed("selectedtext", false);      //remove class selectednode    } else {      text.classed("selectedtext", true);          //adds class selectednode    }  });
.node {    cursor: pointer;  }  .node circle {    fill: #fff;    stroke: steelblue;    stroke-width: 3px;  }  .node text {    font: 12px sans-serif;  }  .link {    fill: none;    stroke: #ccc;    stroke-width: 2px;  }  .selectedtext {    fill: red;  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>  <svg width="960" height="500">    <g transform="translate(120,20)">      <path class="link" d="m0,262.85714285714283c90,262.85714285714283 90,197.1428571428571 180,197.1428571428571"></path>      <path class="link" d="m0,262.85714285714283c90,262.85714285714283 90,328.57142857142856 180,328.57142857142856"></path>      <path class="link" d="m180,197.1428571428571c270,197.1428571428571 270,131.42857142857142 360,131.42857142857142"></path>      <path class="link" d="m180,197.1428571428571c270,197.1428571428571 270,262.85714285714283 360,262.85714285714283"></path>      <g class="node" transform="translate(180,328.5714416503906)">        <circle r="10" style="fill: rgb(255, 255, 255);"></circle>        <text x="13" dy=".35em" text-anchor="start" style="fill-opacity: 1;">level 2: b</text>      </g>      <g class="node" transform="translate(180,197.14285278320312)">        <circle r="10" style="fill: rgb(255, 255, 255);"></circle>        <text x="-13" dy=".35em" text-anchor="end" style="fill-opacity: 1;">level 2: a</text>      </g>      <g class="node" transform="translate(0,262.8571472167969)">        <circle r="10" style="fill: rgb(255, 255, 255);"></circle>        <text x="-13" dy=".35em" text-anchor="end" style="fill-opacity: 1;">top level</text>      </g>      <g class="node" transform="translate(360,262.8571472167969)">        <circle r="10" style="fill: rgb(255, 255, 255);"></circle>        <text x="13" dy=".35em" text-anchor="start" style="fill-opacity: 1;">daughter of a</text>      </g>      <g class="node" transform="translate(360,131.42857360839844)">        <circle r="10" style="fill: rgb(255, 255, 255);"></circle>        <text x="13" dy=".35em" text-anchor="start" style="fill-opacity: 1;">son of a</text>      </g>    </g>  </svg>


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -