Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery Shorthand Re: jQuery Shorthand

#73763
Chris Coyier
Keymaster

I took a crack at it. I can’t test anything of course, so I can’t 100% vouch for it, but this should get you going at much more flexible and DRY code:

// DOM Ready
$(function() {

// Give #nav-fragment-1 through 4 a class name of "nav-fragment"
// Gove #fragment-1 through 4 a class name of "fragment"

var $navFragments = $(".nav-fragment"),
$fragments = $(".fragment");

// MOUSEENTER
$navFragments.hover(function() {

// Get "fragment-2" from "nav-fragment-2"
var relatedID = "#" + this.id.substring(4),
// Make into jQuery object
relatedEl = $(relatedID);

// Remove hiding class name from related fragment
releatedEl.removeClass("ui-tabs-hide");

// Add hiding class name to all other fragments
$fragments.not(relatedEl).addClass("ui-tabs-hide");

// Remove selected class from all other nav-fragments
$navFragments.removeClass('ui-tabs-selected');

// Add selected class to hovered nav-fragment
$(this).addClass('ui-tabs-selected');

// MOUSELEAVE
}, function() {

// Seems like you wouldn't need to do anything here...

});

});