JavaScript D3.js Right click opens popup but does not affect the source svg when pop up closed right click opens link in new tab,right click opens

Here is my d3.js code , from here on right click i am calling the function contextmenu

// Display the tiles for each bucket.
var tile = svg.selectAll(".tile")
    .data(buckets)
    .enter().append("g");

// add rect and rect attributes         
tile.append("rect")
//.enter().append("rect")
.attr("class", "tile")
.attr("x", function (d) {
return x(d.country) + margin.left + margin.left_padding;
})
.attr("y", function (d) {
return y(d.distrinct_port_nme);
})
.attr("width", x.rangeBand())
.attr("height", y.rangeBand())
.attr("stroke", "goldenrod")
.attr("fill", function (d) {
return z(d.sum_teu);
})
.on("contextmenu",contextmenu);

Here is my contextmenu function :

function contextmenu(){
 var node= d3.select(this);
 var position = d3.mouse(this);
d3.select('#my_custom_menu')
  .style('position', 'absolute')
  .style('left', position[0] + "px")
  .style('top', position[1] + "px")
  .style('display', 'block');

d3.event.preventDefault();
document.getElementById('nodeId').value= node
}   

Which opens a pop up with id= my_custom_menu , here is the popup design :

<div id="my_custom_menu" style="display:none;">
<ul> 
<input type="hidden" name="nodeId" id="nodeId" value="" />
 <li onclick="closepop()" style="cursor:pointer;">Close</li>
 <li style="cursor:pointer;">Change color</li>
</ul>
</div

Clicking on the close it closes(hides) the popup :

function closepop(){
d3.select('#my_custom_menu')
 .style('display', 'none');
var selectNode =  document.getElementById('nodeId').value;
selectNode.style("fill", "green");
}

Now on close i tried to set the rectangle from where i right clicked fill green. I tried ti store the instance of the right clicked rectangle and the while closing tried to fill with green against the rectangle. But not working. Can anybody help me with my mistakes. Thanks in advance

Answer:1

In close up function selectNode fetch only the string. Can't call style method ,

Try this code:

Fiddle:

var selectNode =  document.getElementById('cir');
     selectNode.style.fill="red";

For solving your problem , select node as a global variable,then access as your same code.

Fiddle

Answer:2

First of all I set the id correspondent to each element by :

.attr("id", function(d) { return "id_" + parseInt(d.x)+"_"+parseInt(d.y)+"_"+Math.floor((Math.random()*100)+1) })

Then in the function contextmenu I get the id by :

function contextmenu() {
var position = d3.mouse(this);
var node= d3.select(this).attr("id");   
d3.select('#my_custom_menu')
  .style('position', 'absolute')
  .style('left', position[0]+200 + "px")
  .style('top', position[1] + "px")
  .style('display', 'block');

d3.event.preventDefault();
document.getElementById('nodeId').value= node

}

Then in the function closepop

function closepop(){
 d3.select('#my_custom_menu')
 .style('display', 'none');
 var selectN=document.getElementById('nodeId').value;
d3.select('#'+selectN)
.style('fill', 'green');

}

Answer:3

I am developing inside of a Google Chrome Extension. In this environment, I have the ability to inject JavaScript into third-party websites after requesting the appropriate permissions. As such, I am ...

I am developing inside of a Google Chrome Extension. In this environment, I have the ability to inject JavaScript into third-party websites after requesting the appropriate permissions. As such, I am ...

I've having trouble parsing integers or even text from <strong> elements on a page. What I want to do is parse the number of followers from a Twitter profile, like this one. Using Google dev ...

I've having trouble parsing integers or even text from <strong> elements on a page. What I want to do is parse the number of followers from a Twitter profile, like this one. Using Google dev ...

  1. parseint text value

I am trying to set the state (actiu) of multiple components to false, referring to them via the class. How can I achieve that when I click a Label from a row, the actiu state of all the elements in ...

I am trying to set the state (actiu) of multiple components to false, referring to them via the class. How can I achieve that when I click a Label from a row, the actiu state of all the elements in ...

  1. reactjs setstate multiple properties
  2. reactjs setstate multiple
  3. react js multiple setstate

I've generated html and I need to restructuring the html using Jquery, like this: Original: <table id="myTableID"> <tr id="tr01"> <td>Col 1 Value</td> </tr> <tr ...

I've generated html and I need to restructuring the html using Jquery, like this: Original: <table id="myTableID"> <tr id="tr01"> <td>Col 1 Value</td> </tr> <tr ...

  1. jquery move to another page
  2. jquery move to another div
  3. jquery move element another
  4. jquery move element after another
  5. jquery move element to another parent
  6. jquery move element to another div
  7. jquery move element before another
  8. jquery move div after another div
  9. jquery move div to another parent
  10. jquery move element into another
  11. jquery move div before another div
  12. jquery move div into another div
  13. jquery move element inside another
  14. jquery move div inside another div
  15. jquery move node to another parent
  16. jquery move html to another div
  17. jquery move button to another div
  18. jquery move li to another ul
  19. jquery move child to another parent
  20. jquery move tr to another table