HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

jQuery click handlers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
clickjqueryhandlers

Problem

I'm working with Codeigniter and I have a jQuery file to add some functionalities. But I think I'm doing it in a wrong way.

$(function() {
    var list = '../indoamericana/intranet/employeesList/';
    var home = '../indoamericana/administrar/callHome/';
    var profile = '../indoamericana/user/';
    var addUser = '../indoamericana/user/addUSer';

    $(document).on('click', '#documentacion', function(e) { 
        var head = $('iframe').contents().find('head');
        head.append($('', { 
            rel: 'stylesheet', 
            href: '/indoamericana/webroot/css/style.css', 
            type: 'text/css' }));
        head.append($('', { rel: 'stylesheet', 
            href: '/indoamericana/webroot/css/custom.css', 
            type: 'text/css' }));
        e.preventDefault();
    });

    $(document).on('click', 'a[data-target=#ajax]', function(ev){
        var target = $(this).attr("href");
        $("#ajax").load(target, function() { 
            $("#ajax").modal("show"); 
        });
        ev.preventDefault();
    });

    var loadPage = function(page){
        $(".page-content").load(page);  
    };

    $(".sub-menu .menu-item, .module-item").click(function(event) {
        $(".page-content").load($(this).data('target'));  
        $("title").text( $( this ).text() );
    });

    $( document ).on( 'click', '.page-sidebar-menu li', function(e) { 
        $( this ).addClass('active');
        $( this ).siblings().removeClass('active');
    });

    var last_clicked = '';
    var order = 'asc';

    $(document).on('click', '#admin-support a i', function(e){
        var index = $( this ).attr('data-sort'); 
        if( last_clicked == index )
            order = ( order == 'asc' ) ? order = 'desc' : order = 'asc';
        last_clicked = index;
        var page  = '../indoamericana/soporte/allSupports/' + index + '/' + order; 
        $(".page-content").load(page);
    });
});


I'm calling this file (call-functionalities.js) in a script type of my '

Solution

-
Apart from the fact I can't find any use of var loadPage It doesn't need to be an expression.
Instead try:

function loadPage(page) {
}


It is still accessible only within your code here, it is cleaner and easier to debug.

-
Keep your click events consistent

$(".sub-menu .menu-item, .module-item").click(function(event) {
    $(".page-content").load($(this).data('target'));  
    $("title").text( $( this ).text() );
});


Is the only place you're using .click( I'd stick with the $(parent).on('click', '.selector', function(){ style.

Code Snippets

function loadPage(page) {
}
$(".sub-menu .menu-item, .module-item").click(function(event) {
    $(".page-content").load($(this).data('target'));  
    $("title").text( $( this ).text() );
});

Context

StackExchange Code Review Q#40290, answer score: 2

Revisions (0)

No revisions yet.