0

On my page I have two buttons, button 1 and button 2. Under those buttons I want to get the value of a javascript per button, so that I can switch the javascript per button. I am a beginner and I can really use some help for this :) I have tried everything that is possible at my knowledge level. I searched on Stack but didn't find anything close to what I needed. Hopefully you can help me.

EDIT: Ive updated my snippet. Now one button is working en de graph is coming. When i push the second button nothing happens.

function myFunction1() {
  var x = document.getElementById('3d-graph');
    if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
function myFunction2() {
  var x = document.getElementById('graph');
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

    var elem = document.getElementById("graph");
    var drivertwo = neo4j.v1.driver("bolt+routing://90bfd895.databases.neo4j.io", neo4j.v1.auth.basic("got", "got"),{encrypted: true});
 var sessiontwo = drivertwo.session();
 var starttwo = new Date()
    sessiontwo
      .run('MATCH (n)-[r]->(m) RETURN { id: id(n), label:head(labels(n)), community:n.title, caption:n.title, size:n.title } as source, { id: id(m), label:head(labels(m)), community:n.title, caption:m.title, size:m.title } as target, { weight:r.title, type:type(r), community:case when n.title < m.title then n.title else m.title end} as rel LIMIT $limit', {limit: 1000})
      .then(function (result) {
        var nodes = {}
        var links = result.records.map(r => { 
        var source = r.get('source');source.id = source.id.toNumber();
           nodes[source.id] = source;
        var target = r.get('target');target.id = target.id.toNumber();
           nodes[target.id] = target;
           var rel = r.get('rel'); if (rel.weight) { rel.weight = rel.weight.toNumber(); }
        return Object.assign({source:source.id,target:target.id}, rel);
     
     });
         sessiontwo.close();
        console.log(links.length+" links loaded in "+(new Date()-starttwo)+" ms.")
        var gData = { nodes: Object.values(nodes), links: links}
        var Graph = ForceGraph()(elem)
                      .graphData(gData)
                      .nodeAutoColorBy('label')
                              .linkDirectionalParticles('weight')
                      .linkDirectionalParticleSpeed(0.001)
                      .nodeLabel(node => `${node.label}: ${node.caption}`)
                      .onNodeHover(node => elem.style.cursor = node ? 'pointer' : null);
      
 
    
      
      });
   
     

    var elem = document.getElementById('3d-graph');
    var driver = neo4j.v1.driver("bolt+routing://90bfd895.databases.neo4j.io", neo4j.v1.auth.basic("got", "got"),{encrypted: true});
 var session = driver.session();
 var start = new Date()
    session
      .run('MATCH (n)-[r]->(m) RETURN { id: id(n), label:head(labels(n)), community:n.title, caption:n.title, size:n.title } as source, { id: id(m), label:head(labels(m)), community:n.title, caption:m.title, size:m.title } as target, { weight:r.title, type:type(r), community:case when n.title < m.title then n.title else m.title end} as rel LIMIT $limit', {limit: 1000})
      .then(function (result) {
        var nodes = {}
        var links = result.records.map(r => { 
        var source = r.get('source');source.id = source.id.toNumber();
           nodes[source.id] = source;
        var target = r.get('target');target.id = target.id.toNumber();
           nodes[target.id] = target;
           var rel = r.get('rel'); if (rel.weight) { rel.weight = rel.weight.toNumber(); }
        return Object.assign({source:source.id,target:target.id}, rel);
     
     });
         session.close();
        console.log(links.length+" links loaded in "+(new Date()-start)+" ms.")
        var gData = { nodes: Object.values(nodes), links: links}
        var Graph = ForceGraph3D()(elem)
                      .graphData(gData)
                      .nodeAutoColorBy('label')
                              .linkDirectionalParticles('weight')
                      .linkDirectionalParticleSpeed(0.001)
                      .nodeLabel(node => `${node.label}: ${node.caption}`)
                      .onNodeHover(node => elem.style.cursor = node ? 'pointer' : null);
      
 
    
      
  
      
      });
<script src="https://unpkg.com/force-graph"></script>
 <script src="https://unpkg.com/3d-force-graph"></script>
 <script src="https://cdn.rawgit.com/neo4j/neo4j-javascript-driver/1.2/lib/browser/neo4j-web.min.js"></script>



<button onclick="myFunction1()">Try it</button>
<button onclick="myFunction2()">Try it</button>
<p id="graph"></p>
<p id="3d-graph"></p>
  • 1
    show us your code – xmaster Jun 18 '19 at 11:23
  • Hello Bennie, welcome to Stack Overflow. Please edit your question and include code from your failed attempt. This will make it much easier for us to help – Patrick Hund Jun 18 '19 at 11:24
  • You need to show enough of the source for page to show what problem you are having. – stark Jun 18 '19 at 11:25
  • Sorry. Ive edited my post with some code:) If have this code on a html page. –  Jun 18 '19 at 11:30
  • You can pass ``this`` instead of ``elem`` as parameter to myFunction. Possible duplicate of https://stackoverflow.com/questions/4268085/the-current-element-as-its-event-function-param – www.admiraalit.nl Jun 18 '19 at 11:42
  • @www.admiraalit.nl ive tried it and it dont work for me. Thanks tho :) –  Jun 18 '19 at 12:03
  • Your HTML contains a ``

    `` with two ids. You should have two separate ``

    `` tags, each with its own id.

    – www.admiraalit.nl Jun 18 '19 at 12:09
  • The second button has ``id2='2'``; this should be ``id='2'``. – www.admiraalit.nl Jun 18 '19 at 12:12
  • @www.admiraalit.nl Ive updated my post. Hopefully you got another tip for me :) –  Jun 18 '19 at 13:19
  • Do you use a JavaScript debugger? If you don't, please add ``alert``-statements or ``console.log``-statements to see exactly which lines of code are executed. Is myFunction2 called at all when you press the second button? – www.admiraalit.nl Jun 18 '19 at 13:30
  • Note that myFunction2 consists of only 8 lines. Should the code starting with ``var elem = `` be part of myFunction2? Then your braces ``}`` are not correctly placed. – www.admiraalit.nl Jun 18 '19 at 13:38
  • Thank you for helping out. Ive added console.log but i dont see something wrong. Its indeed the second button 'myfuction2' who is doing nothing. When the graph is loading, both are loading but only one is showing and the second one is gone.. –  Jun 18 '19 at 13:41
  • What do you mean by 'doing nothing'? Does it execute your console.log statements? – www.admiraalit.nl Jun 18 '19 at 13:59
  • @www.admiraalit.nl sorry i can't seem to find the problem. Hopefully you got another tip for me. –  Jun 18 '19 at 13:59
  • If you press button2, does it enter myFunction2 ? – www.admiraalit.nl Jun 18 '19 at 14:04
  • @www.admiraalit.nl Sorry, I am still a beginner and glad you take the time to help me. No, nothing happens. Button 1 works and retrieves the graph and hides it again. Button 2 does nothing. All scripts are launched and no error comes up –  Jun 18 '19 at 14:07
  • I think myFunction2 is called correctly, but the graph isn't loaded. If you replace ``

    `` by ``

    this is graph

    ``, then you will notice that button2 hides and shows the text "this is graph". See https://jsfiddle.net/dftsx69r/ Do you agree?
    – www.admiraalit.nl Jun 18 '19 at 14:21
  • Yea i agree. Its works and i see the text. So why dont i see my second graph when i hit button2?@www.admiraalit.nl –  Jun 18 '19 at 14:26
  • So my conclusion is that your original question is answered: you are able to have two different js under two different buttons, right? Now the reason you don't see the second graph is that you have declared ``var elem`` twice. For the second image, declare ``var elem2`` and use elem2 inside ``ForceGraph3D`` (two spots). See https://jsfiddle.net/dftsx69r/3/ for the result. – www.admiraalit.nl Jun 18 '19 at 14:41
  • Note, that an image is not loaded after you click a button, but immediately when the page is loaded. But the image is initially hidden and the button just reveals the hidden image. – www.admiraalit.nl Jun 18 '19 at 14:48
  • This is exactly what I was looking for. Thank you so much for this :) @www.admiraalit.nl –  Jun 18 '19 at 14:53
  • Please check my answer below as accepted, so that I receive credit points for helping you out. – www.admiraalit.nl Jun 18 '19 at 14:57
  • Benny, please click on the checkmark next to my answer below. This indicates that my answer helped you out and it will give me 25 reputation points. – www.admiraalit.nl Jun 19 '19 at 06:52
  • I did :) Thank again. –  Jun 19 '19 at 08:20

2 Answers2

1

The buttons work correctly. The reason you don't see one of the images is that you declared var elem twice.

If you replace elem by elem2 in var elem = document.getElementById('3d-graph'); and in the code below that, then it will be fixed.

But it would be better to wrap the image loading in a separate function, like this:

function loadImage(elementId) {
    var elem = document.getElementById(elementId);
    ...
    ...
}

and then call this function twice:

loadImage('graph');
loadImage('3d-graph');
www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
  • It's just to eliminate some code duplication. I advise you to gain some basic knowledge of JavaScript and then you would be able to do it yourself soon enough. – www.admiraalit.nl Jun 19 '19 at 10:06
0

try this and let me know if there are other problems:

<button id='1' onclick="myFunction(this)">button 1</button> 
<button id='2' onclick="myFunction(this)">button 2</button> 
<p id="graph"></p>
<p id="3d-graph"></p>

Passing "this" to myFunction, the "elem", will be the button you click. Remember that is better to use .addEventListener('click', function(ev){}) instead "onclick=function()"

function myFunction(elem) {
    document.getElementById("graph").innerHTML = "ID:" + elem.id;
    document.getElementById("3d-graph").innerHTML = "ID:" + elem.id;
}

I don't know what the code below what the rest of the code will do, and if it works, but i see a lot of duplications. If you can, try to put all the neo4j call inside a function, so that you can use this every time you need it.