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.

0

1 Answer 1

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;'
})
Sign up to request clarification or add additional context in comments.

3 Comments

Why use innerHTML, when you could instead append elements via DOM? You get linter/checker support that way.
@SamHughes Just because OP asked for a one-line solution.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.