How to set the placeholder attribute within a nested element in Javascript

Viewed 299

This code creates a nested input within a list element but the placeholder doesn't display:

var li = document.createElement('li');
            this._new_input_tag = document.createElement('input');
            li.className = 'tagger-new';
            li.placeholder = "Write here";

I tried

document.getElementById('tagger-new').placeholder = "Write here";

but that removed the input entirely.

Thanks for your help!

2 Answers

you need to use append child run snippet below

function myFunction() {
  var node = document.createElement("LI");
  var inputnode = document.createElement("input");
  inputnode.placeholder = "Write here"
  node.appendChild(inputnode);
  document.getElementById("myList").appendChild(node);
}
<ul id="myList">
</ul>

<p>Click the button to append an input to the end of the list.</p>

<button onclick="myFunction()">Try it</button>

the reason why there is error in you code you put there something which is don't clear this key word while you were creating element

input = document.createElement('input');
input.setAttribute('placeholder', 'Write Here...');
document.body.appendChild(input)

Related