0

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!

2
  • 2
    The items are being added after the listeners are attached, meaning the listeners aren't being subsequently attached to them too Commented Sep 17, 2014 at 15:12
  • I tried adding list = document.getElementsByTagName("li"); after adding the new HTML but no luck. Any idea? Commented Sep 17, 2014 at 15:14

4 Answers 4

1

You sure found an excellent way to complicate that one

$('#result').on('click', 'li', function() {
    $(this).css('font-weight', function(_, w) {
        return w === 'bold' ? 'normal' : 'bold';
    });
});

$('#result').html("<ul><li>apple</li><li>orange</li><li>banana</li></ul>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>

Sign up to request clarification or add additional context in comments.

4 Comments

"You sure found an excellent way to complicate that one" lol thanks for the help. As you can tell I'm new to web dev!
Learning plain javascript is good, and it helps a lot, but if you're already using jQuery, try sticking to most of the simple methods it provides, it makes it a lot easier to write good code.
Gotchya, will def start to use as much jQuery. Thanks for the tip
I didn't know you could run/include code snippets. That is just too cool.
1

Use full power of jQuery:

$('#result').on('click', 'li', function() {
    $(this).toggleClass('bold');
});

In this case the event will fire for all li elements inside #result even if they were created dynamicaly. Also toggling css class will save some space.

http://jsfiddle.net/semencov/kn0w1y8m/

Comments

0

You can use this :

var list = document.getElementsByTagName("li");

function markSelection() {
    if (this.style.fontWeight !== "bold") {
        this.style.fontWeight = "bold";
    } else {
        this.style.fontWeight = "normal";
    }
}
$('body').on('click', 'li', markSelection);

It will make jquery add a click event on every li element that are child of the body element.

Comments

0

Try this, First add listelement to html and then apply click event:

$(document).ready(function () {
        document.getElementById("result").innerHTML = "";
        $(result).html("<ul><li>apple</li><li>orange</li><li>banana</li></ul>");
    });

 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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.