Print values from an array

1

I want to print the values of an array that are pushed when I click an object. The function works because the alerts appear. 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>
    
asked by anonymous 03.12.2014 / 23:28

1 answer

1

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

  

array.push (newCount);

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 use JSON.stringify(array)

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

Or use array.join('separador') :

document.getElementById("demo").innerHTML = "x=" + positionX.join(', ');
    
03.12.2014 / 23:36