I am trying to add new elements in JavaScript, and when I hover my mouse over them I want to see how many letters are in the innerHTML. The function works on elements added in html, but the new ones not.
var input = document.getElementById("input")
var button = document.getElementById("button");
button.onclick = function () {
var p = document.createElement("p");
p.innerHTML = input.value ;
document.body.appendChild(p);
p.id="p";
}
var ar = document.getElementsByTagName("p");
for (var i = 0; i < ar.length; ++i) {
ar[i].title=ar[i].innerHTML.length;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input id="input"></input>
<button type="button" id="button"> click</button>
<p>p1</p>
<p>p2</p>
</body>
</html>
What I want to achive: number of letters of element I hover on,
What I get: number of letters of elements I hover on, but only these that were added in html.