Is there any way to add a text to a created div

Is there any way to add a text to a created div ?

I’m creating a div like this

I want to add a text to this div

This might help - https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText

Short answer, as Chris suggests, use .innerText (or innerHTML):

var newDiv = document.createElement('div');
newDiv.id = 'container';
newDiv.style.minWidth = "310px";
newDiv.style.height = "400px";
newDiv.style.margin = "0 auto";
$('body').append(newDiv);
newDiv.innerHTML = "<p>Hello</p>"

Slightly longer answer: if you’re including jQuery, you might as well make use of it.

var newDiv = $('<div>', {
  id: 'container',
  css: {
    minWidth: '310px',
    height: '400px',
    margin: '0 auto'
  },
  text: 'Lorem Ipsum'
}).appendTo('body');

I am doing this in handlebar

<script>
              .............................
		newDiv.innerHTML = "<p>{{masterArr[i].createdOn}}</p>"
</script>

//symbols are standard.

I get error

Parse error on line 206: …v.innerHTML = "<p>{{masterArr[i].created -----------------------^ Expecting ‘ID’, ‘STRING’, ‘NUMBER’, ‘BOOLEAN’, ‘UNDEFINED’, ‘NULL’, ‘DATA’, got ‘INVALID’

facing this syntax trouble.

I would suggest employing some of the debugging techniques outlined here.

1 Like

Hi there winzip,

here is your forked “fiddly thing”

https://jsfiddle.net/ahcmtyvd/

coothead

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.