-2

im a beginner in javascript, my intention is to pass child value gathered from each websocket data to another function to be showed in alert window.

this is my code,

      function insertRow(x,y){
        var tr      = document.createElement("tr"),
            tdCoin  = document.createElement("td"),
            tdPrice = document.createElement("td"),
            tdPrice2 = document.createElement("td"),
            tdActionApprove = document.createElement("button"),
            tdActionReject = document.createElement("button"),
            docFrag = new DocumentFragment();
    
        var today = new Date();
        var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    
            tdCoin.textContent = time;
            tdPrice.textContent = `${Number(x)}`;
            tdPrice2.textContent = `${Number(y)}`;
            tdActionApprove.innerText = 'Approve';
            tdActionReject.innerText = 'Reject';

            var xPrice = Number(x)
            
            tr.appendChild(tdCoin);
            tr.appendChild(tdPrice);
            tr.appendChild(tdPrice2);
            tr.appendChild(tdActionApprove);
            tr.appendChild(tdActionReject);

            tdActionApprove = document.getElementById('button').addEventListener('click', function() {
              loadScriptApprove(xPrice);
            }, false);
            //tdActionApprove.onclick = loadScriptApprove(xPrice);
            //tdActionReject.onclick = loadScriptReject;
    
            docFrag.appendChild(tr);
            return docFrag;
    }

    function loadScriptApprove(xPrice){
      alert('Approve Button Clicked ' + xPrice)
    }

    function loadScriptReject(){
      alert('Reject Button Clicked')
    }
  
      var awdSocket = new WebSocket("ws://simple-websocket-server-echo.glitch.me/"),
          table = document.getElementById("WdTable");

          awdSocket.onmessage = function(event) 
          {
              var messageObject = JSON.parse(event.data);
              table.appendChild(insertRow(messageObject.x, messageObject.y));
          }

somehow the alert is showing value passed but the button is not functioning properly, meaning when the new websocket value is passed the button is autoclicked.

i tried using bind using this as recommended but still it showing an error

                tdActionApprove = document.getElementById('button').addEventListener('click', function() {
              loadScriptApprove(xPrice);
            }, false);

is there any better solution to this ?

much thanks and regards,

0 Answers0