Grow your CSS skills. Land your dream job.

jquery on click events

  • # March 11, 2013 at 1:04 pm

    is this proper?
    instead of grabbing an element and binding it with click i now use 2 classes.
    class toggle and class click.
    this way I can add it anywhere and as much as I want. Is that bad?

    $(‘.click’).on(‘click’, function(){

    //inner div.toggle opens
    $(this).find(‘.toggle’).slideToggle();

    }

    return false;

    });

    # March 11, 2013 at 1:36 pm

    I would say that using a class of ‘click’ is pretty bad, but otherwise this doesn’t look too bad.

    # March 11, 2013 at 2:08 pm

    well when I use jquery I use special classes. that way my javascript is neat.
    I care more for my js being neat than my html because js can get cumbersome.

    # March 12, 2013 at 2:44 am

    If the amount of clicks will be ‘insignificant’ than you can simply use .click(function() {. I would agree with doc that the naming conventions are a quite un-semantic. Whenever i am using a lot of JS i normally give the element an id and prepend ‘js-’.

    I suggest you give this article a good read.

    # March 12, 2013 at 7:27 am

    > If the amount of clicks will be ‘insignificant’ than you can simply use `.click(function() {`.

    Please elaborate :P

    # March 12, 2013 at 10:55 am

    .click ( );

    example:



    $('.foo').click(function () {
    // do stuff here
    });

    # March 12, 2013 at 5:42 pm

    @CrocoDillon

    From jQuery: Event Performance

    # March 12, 2013 at 6:09 pm

    Ah thanks, good read. I thought you meant `.click(…)` to be less efficient then `.on(‘click’,…)`. Ah well I agree that class names like ‘click’ are not semantic and I wouldn’t use it except in testing environment.

    # March 15, 2013 at 5:08 pm

    yeah but if I use class .toggle, I dont have to write anymore code!
    I just add class toggle and bam! -that element now has jquery attached to it.
    Its similar to oocss… I guess…
    instead of having to put a few elements into the $(), i put one element (.toggle) and place toggle here and there. And its easy for me to search and see whats being toggled.

    Otherwise im pretty semantic except for cf (clearfix) my html is pretty good…i think…

    and click() just calls the function on(‘click’..) so I use on(‘click’) to begin with.

    ‘As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers….’

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".