Forums

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

Home Forums JavaScript jquery on click events

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #43305
    shamai
    Member

    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;

    });

    #127760
    TheDoc
    Member

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

    #127763
    shamai
    Member

    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.

    #127819
    JohnMotylJr
    Participant

    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.

    #127821
    CrocoDillon
    Participant

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

    Please elaborate :P

    #127889

    .click ( );

    example:



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

    #127959
    JohnMotylJr
    Participant
    #127963
    CrocoDillon
    Participant

    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.

    #128415
    shamai
    Member

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.