Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript createElement, add html element and simple text

I am trying to create this html block in javascript:

<label>
  <input name="{{ $amenity->name }}" type="checkbox" class="minimal">
  text
</label>

I have:

var checkBox = document.createElement("input");
var label_input = document.createElement("label");
label_input.innerHTML = value.name;
label_input.appendChild(checkBox);

But the result is

<label>
  text
  <input name="{{ $amenity->name }}" type="checkbox" class="minimal">
</label>

and I tried to add the checkbox first and then the text but no success.

like image 265
Sebastian Corneliu Vîrlan Avatar asked Mar 12 '23 13:03

Sebastian Corneliu Vîrlan


1 Answers

Try using document.createTextNode for appending text content to the end of the label.

var checkBox = document.createElement("input");
var label_input = document.createElement("label");
label_input.appendChild(checkBox);
label_input.appendChild(document.createTextNode(value.name));
like image 121
Jeff Jenkins Avatar answered Apr 09 '23 21:04

Jeff Jenkins