1

`<div class="check"><input type="checkbox" name="buy" value="260" checked="" onclick="javascript:basket.checkItem();">&nbsp;</div>

this is element that I want to make by createElement(). I want to know is there anyway complete this in one command? length doesn't matter.

1 Answers1

7

If you want a one-liner, you could use:

Object.assign(document.createElement('div'), {className: 'check', innerHTML: '<input type="checkbox" name="buy" value="260" checked="" onclick="javascript:basket.checkItem();">&nbsp;'})

Or, better readable:

Object.assign(document.createElement('div'), {
  className: 'check',
  innerHTML: '<input type="checkbox" name="buy" value="260" checked="" onclick="javascript:basket.checkItem();">&nbsp;'
})
eyecatchUp
  • 10,032
  • 4
  • 55
  • 65
  • Why use innerHTML, when you could instead append elements via DOM? You get linter/checker support that way. – Sam Hughes Mar 28 '22 at 19:28
  • @SamHughes Just because OP asked for a one-line solution. – eyecatchUp Mar 31 '22 at 05:09
  • I mean, the arbitrary one-line restriction is as crazy as it is unconstrained. Using an IIF approach, you've got it created, properties assigned, and then same for each child. for instance: `toldYouSo = (eMaker, head, ...fam) => {head = eMaker(head); fam.forEach(c=>head.appendElement(eMaker(c)); return head;})(tagData=>object.assign(document.createElement(tagData.tag, tagData), {tag: "div", className: "check"}, {tag: "input", type: "checkbox", name: "buy", value: "260", checked: "", onclick: "javascript:basked.checkItem();"})` ... Lordy, I miss JS. Typechecking is nice, but Golang is hell. – Sam Hughes Apr 01 '22 at 15:25