i am trying to use innerHTML to insert some data into an html page like folowing
for(i = 0; i < data.length; i++){
var line = data[i].split("~");
nameslist.innerHTML += '<p onClick='+userclick(line[1])+'><strong>' + line[0] + ' </strong></p>';
}
but onclick event fired automatically directly after the insert and it wont fire when i click on the <p> element
how do i use innerhtml properly to add a p element with onclick event and fire it only when user click ?
'<p onClick="userclick(line[1])">createElement,appendChildandaddEventListenerinstead. It's more verbose but much easier to maintain.setAttributeafterward. developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute<p>elements aren't designed to be interactive. You can add a click event handler to them, but then someone comes along and can't use a mouse but they can't focus the<p>because it isn't designed to be a UI control. Use appropriate elements (e.g.<button>).