Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a HTMLdivElement object in Javascript

Tags:

javascript

Trying to turn a String into a HTMLDivElement object. I know how to create a string, I am just unsure how to create the HTMLDivElement.

For example, if I wanted to turn <div id="cardId" class="card c9"></div>

It would also be good to really understand what a HTMLDivElementObject is too.

EDIT:

I also know that there are Universal Attributes such as id(string), style(object), title(string) and className(string).

Below is my function and method. As you can see I try to create the toElement method. What I need is the method to return an HTMLDivElement object representing the Card. The example of what needs to be returned is as above <div id="cardId" class="card c9"></div>.

var cardProto = {
// creates the HTML string representing this element
    "toString": function (){
        var s = this.card.getSuit(), r = this.card.getRank();
        var t = "<div class=\"card ";
        t += s !== undefined ? s + r : "down";
        t += "\" id=\""+ this.id + "\"></div>\n";
        return t;
    },

    "toElement": function () {


    }
};
like image 708
TheCrownedPixel Avatar asked Sep 03 '25 03:09

TheCrownedPixel


1 Answers

"toElement": function () {
  var dummy = document.createElement("div");
  dummy.innerHTML = this.toString();
  return dummy.children[0];
}
like image 142
lloiser Avatar answered Sep 04 '25 18:09

lloiser