For beginners, it's important to consider the benefits of keeping things modular by separating content, style, and functionality. It is advisable not to include an onClick directly in the HTML.
HTML
<div class='message-block [ js-do-something ]'>
<p class='greeting'>Hi!</p>
<p class='message'>I'm Noah</p>
</div>
JavaScript
Storing functions in variables is a good approach, especially if you need to reuse the functionality in multiple places. By passing parameters into the function, you can customize each function call for different elements. Avoid unnecessary specificity in ID names - using classes is generally more efficient. To visually separate classes dedicated to functionality, consider prefixing them with 'js-' or enclosing them in brackets. These are just some tips to optimize your code.
var showStuff = function(input) {
var $input = $(input);
var fadeTime = 300;
$input.on('click', function() {
$input.find('.greeting').animate({
opacity: 1
}, {
duration: fadeTime,
complete: function() {
$input.find('.message').animate({
opacity: 1
}, fadeTime);
}
});
});
};
showStuff('.js-do-something');
Check out the example on jsFiddle