I have a form, I would like to dynamically build, such that each product that has a help text gets a tooltip. This is a Product. It extends BaseProduct:
class BaseProduct {
constructor(name, helpText = null) {
this.name = name;
this.helpText = helpText;
}
label() {
if (this.helpText) {
return `
<label class="tooltip">
${this.name}
<div class="right">${this.helpText}</div>
</label>`;
}
return `<label for="not-important">${this.name}</label>`;
}
}
class Product extends BaseProduct {
constructor(name, price, helpText = null) {
super(name, helpText);
this.price = price;
}
toHtml() {
let inner = `
<input type="checkbox" name="not-important"
id="not-important" value="not-important" />
${super.label()}
<span class="price">${this.price}</span>`;
return `
<p class="item-row">
${inner}
</p>`;
}
}
You can check it out here: https://jsfiddle.net/whoru74c/10/
There is one example that is not dynamic, showing what it is supposed to look like. The other Product gets dynamically added.
The strange part about the Product is that I can print the result of calling Product.toHtml()
or super.label()
and it looks like expected:
<label class="tooltip">
Product Name
<div class="right">[Product help text]</div>
</label>
But when I insert it into the DOM tree, using insert()
, it gets split up, so that the <div class="right">
comes after </label>
:
<p class="item-row">
<input type="checkbox" name="not-important" id="not-important" value="not-important">
<label class="tooltip">
Product Name
</label>
<!-- 1 -->
</p>
<!-- The following should be in 1 -->
<div class="right">[Product help text]</div>
<span class="price">123</span>
<p></p>
Furthermore, a random empty <p>
-tag gets added, and I cannot figure out why.