+ 1
Can a div element be created using JS then assigned to an existing class using a constructor function?
I am trying to do this for my Match Three game to create new pieces without having to copy and paste the code from fillBoard to re-assign the properties such as locID and colorID. https://code.sololearn.com/WIGYx83rvJhh/?ref=app
2 Respostas
+ 1
Love that you're still going at this, awesome dedication.
You could create a
function constructPiece(p, locId) {
p.locId = locId;
...
switch (colorId)
....
}
which does all the code that's currently in fillBoard, but only for a single piece.
Then the fillBoard function is reduced to
for(var x = 0; x < 25; x++) {
var p = document.createElement("div");
constructPiece(p, x);
}
And of course you can also reuse the `constructPiece` function someplace else. :)
+ 1
Create an element and assign a class to it, is this what you mean?
https://www.w3schools.com/jsref/met_document_createelement.asp
https://www.w3schools.com/jsref/prop_html_classname.asp