I am trying to add an onKeyPress event to a dynamically created html element using JavaScript. In practise, the element is added, the id is assigned, and the innerHTML added, but the onmouseover, onmouseout, and onKeyPress (2nd last line) events are not added to the element. The last line (.focus()) does work.
Code:
function newParagraphAfter(elem)
{
blockElemId++;
newPara = document.createElement("p");
newPara.id = 'block_' + blockElemId;
newPara.contentEditable = 'true';
newPara.onmouseover = "this.style.border='1px dashed white';";
newPara.onmouseout = "this.style.border='none';";
newPara.innerHTML = "Edit Here!";
elem.parentNode.insertBefore(newPara, elem.nextSibling);
document.getElementById('block_' + blockElemId).onKeyPress = "return editKeypress(this, event)";
document.getElementById('block_' + blockElemId).focus();
}
Any help greatly appreciated.