The o shortcut


#1

var main= function(){
$('.article').click(function(){
$('.article').removeClass('current');
$['.description').hide();

    $(this).addClass('current');
    $(this).children('.description').show();
});

$(document).keypress(function(event) {

if(event.which === 111) {
$('.current').children('.description').toggle();
}
});
};
$(document).ready(main);

That's my code. It doesn't work. Can anybody tell me the correct code?


#2

This is the correct code. Your code is very messy, so it's quite difficult to discern what you're doing there. I'd advise using TAB a lot more often.

var main = function () {
    $('.article').click(function() {
        $('.description').hide();
        $(this).children('.description').show();
        $('.description').removeClass('curent');
        $('.article').removeClass('current');
        $(this).addClass('current');
    });
    $(document).keypress(function(event) {
        if (event.which === 111) {
            $('.current').children('.description').toggle();    
        }   
    });
};

$(document).ready(main);

This is 2 mini functions within the one big MAIN function. Since only the main function is called in $(document).ready() you must put all your code in there.

The basic pattern here is closing everything else, selecting $(this) and adding a class. For example, we hide everything with the description class and then show this description, we remove the '.current' class from all the articles and then apply it to only $(this) article.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.