So, right now I have an html list and some javascript code to go with it. When an item in this list is clicked, it is bolded. This all works fine.
<ul>
<li>apple</li>
<li>orange</li>
<li>banana</li>
</ul>
<!--result is an empty div, for now -->
<div id="result"></div>
<script>
var list = document.getElementsByTagName("li");
function markSelection() {
if (this.style.fontWeight !== "bold") {
this.style.fontWeight = "bold";
} else {
this.style.fontWeight = "normal";
}
}
for (i = 0, len = list.length; i < len; i++) {
list[i].onclick = markSelection;
}
</script>
But, when I add some html to a div using javascript:
$(document).ready(function () {
document.getElementById("result").innerHTML = "";
$(result).html("<ul><li>apple</li><li>orange</li><li>banana</li></ul>");
});
the onclick bolding doesn't work for the items in this list. I'm not sure why. Here is a fiddle to show what I mean. The "dropdown" list isn't clickable but the bottom list is. http://jsfiddle.net/avfyvk2v/11/
Any idea how to make the dropdown list item bold on click?
UPDATE
I'm thinking it might be because I grab var list = document.getElementsByTagName("li"); before I add the new list. Working now, let me know if you have input. Thanks!
list = document.getElementsByTagName("li");after adding the new HTML but no luck. Any idea?