Print values from an array

Asked

Viewed 1,904 times

1

I want to print the values of an array that are pushed every time I click on an object. The function works because Alert appears. The problem is that it does not print the values in the html paragraph.

    var x1;
    var y;
    shape.onclick = function(){
        var name=this.id;
        alert(name);
        x1=this.getAttributeNS(null, "cx");
        y=this.getAttributeNS(null, "cy");
        alert(y);

        namecirc=push(name);
        positionX=push(x1);
        positionY=push(y);

        document.getElementById("demo").innerHTML ="x="+positionX;


    };

html:

<p id="demo"></p>
  • Where are these "arrays" located? For example: positionX is already an array? if it is should use not equality but yes positionX.push(x1); There’s more code missing here?

  • Yes is an array set on top of the code, var positionX = new Array();

  • And what do you want to show in demo? the array with all or only the last positions?

  • all positions, changed to positionX.push(X1), still does not show.

1 answer

2


The .push() is a native Array method, you should use it like this:

array.push(newConference);

So change the code to:

namecirc.push(name);
positionX.push(x1);
positionY.push(y);

To display an array in HTML you have several options.

Or uses JSON.stringify(array)

    document.getElementById("demo").innerHTML = "x=" + JSON.stringify(positionX);

Or uses array.join('separador'):

document.getElementById("demo").innerHTML = "x=" + positionX.join(', ');
  • I cannot show, I have here the complete code http://jsfiddle.net/zp0vcc7c/

  • @akm Remova // from here: //var namecirc =new Array();, this is how it will be: http://jsfiddle.net/cpyq7a8v/

  • 1

    Thank you, it already works

  • @akm if the answer was useful can also vote on it, so contributes in "virtual points" to those who helped solve the problem :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.