Alright, so I’m making a Facebook-style chat. But that doesn’t really matter.
See here:
http://jsfiddle.net/SkHme/7/
Nice and pretty, right? Well, there’s a problem. Notice this line:
<div class="conversation EmperorCuzco" onclick="setActive('EmperorCuzco')">
See the onclick attribute? Well, it’s not working. However, I have confirmed that the function itself DOES work. (if you run it just like that in the JavaScript, it runs like a dream) I have further confirmed that the function is not the problem by attempting to replace the onclick value with a simple alert('blah'), but that doesn’t work either.
So, what’s up? I’m guessing that something in my JavaScript is somehow disabling something, but I have absolutely no idea what it could be, nor how I could go about fixing it. I did some web searching, but couldn’t find anything that helps.
What’s going on?
Your
setActivefunction is defined inside the scope of the$(document).readyhandler. Move the function outside that function so that it is in the global scope.Right now it looks like this:
Now change that to:
Really though, you should separate your content from your interactions and bind those event handlers in your script itself. Something like:
Some advantages of this approach:
$active_conversation, the conversation can be identified without any extra processing.conversationwithout assigning new event handlers. In the sample above, the event handler for all.conversation_buttonelements is defined at the level of#chat. For more about this binding mechanism, read up on.on(or.delegatefor pre-1.7).Here, have an updated fiddle! 🙂