0

The following code snippet is from jquery.graphviz.svg There is an svg image inside a div called "graph". When the div content is replaced with another svg the click event registered here is lost.

$(document).ready(function(){
                $("#graph").graphviz({
                    url: "demo.svg", 
                    ready: function() {
                        var gv = this
                        gv.nodes().click(function () {
                            var $set = $()
                            $set.push(this)
                            $set = $set.add(gv.linkedFrom(this, true))
                            $set = $set.add(gv.linkedTo(this, true))
                            gv.highlight($set, true)
                            gv.bringToFront($set)
                        })
                        $(document).keydown(function (evt) {
                            if (evt.keyCode == 27) {
                                gv.highlight()
                            }
                        })
                    }
                });
            });

I tried changing "gv.nodes().click(function () {" to the following without luck

$(document).on("click",gv.nodes(),(function () {...}

Is there a different way to solve this?

UPDATE:

With a mutation callback, an alert works within the callback, but re-registering the click event is not working:

var gv = $('#graph').data('graphviz.svg')    
gv.nodes().off().on('click',function () {alert('entered doc clck')
Tims
  • 627
  • 7
  • 19
  • 1
    Take the routine that actually attaches the event to the image, and wrap it in a function. Run the function on page load for the initial attachment, and again when the image is replaced. – Stephen R Sep 27 '19 at 00:06
  • Also see [Detect changes in the DOM](https://stackoverflow.com/questions/3219758/detect-changes-in-the-dom). – showdev Sep 27 '19 at 00:28

0 Answers0