Create canvas with ID according to array

2

I have the following code and then in a function out of all these functions I want to do a getElementById, but the created canvases do not have ID, they told me that I should add it to id inside the function per, but I'm disoriented, if someone knows please help.

Code :

  $(document).ready(function(){

        function per() {
            console.log("per()");
             var $canvas = $('<canvas></canvas>').css({
                'border-radius': '5px',
                'padding': '0',
                'margin': '0',
                'width': '200px',
                'height': '200px',
                //'position': 'absolute',
                'right': '15px',
                'bottom': '15px',
                'background-color': 'blue'
            });
          $('body').append($canvas);

        }

        $('body').append('<canvas id="minimap"></canvas>');

            $('#minimap').css({
                'background': 'rgba(1,1,1,0.7',
                'border-radius': '0px',
                'border': '1px solid rgba(255,255,255,0.2)',
                'padding': '0',
                'margin': '0',
                'width': '200px',
                'height': '200px',
                'position': 'absolute',
                'right': '15px',
                'bottom': '15px'
            });
           b.forEach(per);


    });

function obtener() {

document.getElementById(que hago?)
}
    
asked by Eduardo Sebastian 04.05.2017 в 15:31
source

1 answer

1

In the creation of the canvas:

var $canvas = $('<canvas></canvas>').css({

you add the ID.

var $canvas = $('<canvas id="ID"></canvas>').css({

but keep in mind that the ID has to be unique so in the function per you should receive the ID you want to put:

function per(id) {
...
var $canvas = $('<canvas id="' + id + '"></canvas>').css({

and on the call:

b = [1,2,3]; //por ejemplo

b.forEach(function(elem) {
    per("subcanvas" + elem); //id quedaria "subcanvas1", "subcanvas2", ...
});
    
answered by 04.05.2017 / 16:06
source