Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
286 views
in Q2A Core by

Kai's question about Javascript reminded me of an issue I've been meaning to bring up. It's regarded as very bad practice to use event attributes like onmouseover or onclick directly in the HTML. We should use "unobtrusive Javascript", where we have a script block that does all that. This has the added effect of reducing HTML bloat.

Taking the mousover JS as an example, instead of a dozen upvote buttons with the attributes, you can do every one on the page all together with jQuery:

<script>
$('.qa-vote-first-button').hover(
  function() { $(this).addClass('qa-vote-up-hover'); },
  function() { $(this).removeClass('qa-vote-up-hover'); }
);
</script>

A similar thing can be done on click:

<script>
$('.qa-vote-first-button').click(function() {
  return qa_vote_click(this);
});
</script>

What do you think?

Q2A version: 1.5.3
by
+1 for second example and general use!

Seeing addClass and having the usecase for a mouse-hover event that changes design, no, just use css :)
by
Yes I still agree that the hover effects should use pure CSS. I don't see a reason for JS backup on those.

Please log in or register to answer this question.

...