-1

<!DOCTYPE html>
<html>

<body>

  <p>Click the button to create an Input field.</p>

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

  <script>
    function myFunction() {
      var y = document.createElement("LABEL");
      var yv = prompt("Enter Caption value:", "");
      y.setAttribute("value", yv);
      document.body.appendChild(y);

    }
  </script>

</body>

</html>

I want to create 'lebel' using the above code. it's not working. plz help...

Rajesh
  • 24,354
  • 5
  • 48
  • 79
Anup
  • 15
  • 1
  • 1
  • 3
  • 2
    Label does not have a attribute `value`. Use `y.textContent = yv` – Rajesh Nov 06 '17 at 14:23
  • If you use your browser's debugging tools and view the HTML on the page, you'll find that your code *does* in fact successfully create a ` – David Nov 06 '17 at 14:25

2 Answers2

5

function myFunction() {
  var y = document.createElement("LABEL");
  var yv = prompt("Enter Caption value:", "");
  y.innerHTML = yv;
  document.body.appendChild(y);
}
<p>Click the button to create an Input field.</p>

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

use innerHTML to add content.

Durga
  • 15,263
  • 2
  • 28
  • 52
  • Please note that it is a bad practice to answer a duplicate. You should rather use your vote and close it – Rajesh Nov 07 '17 at 02:25
0

You forgot a simple thing myfriend, put someting inside the created label like in the snippet below

<!DOCTYPE html>
<html>

<body>

  <p>Click the button to create an Input field.</p>

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

  <script>
    function myFunction() {
      var y = document.createElement("label");
      var yv = prompt("Enter Caption value:", "");
      y.setAttribute("value", yv);
      y.innerText = yv;
      document.body.appendChild(y);

    }
  </script>

</body>

</html>
FrontTheMachine
  • 526
  • 3
  • 14