Home › Forums › JavaScript › jquery on click events
- This topic is empty.
-
AuthorPosts
-
March 11, 2013 at 1:04 pm #43305
shamai
Memberis 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 #127760TheDoc
MemberI 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 #127763shamai
Memberwell 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 #127819JohnMotylJr
ParticipantIf 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 #127821CrocoDillon
Participant> 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 #127889Historical Forums User
Participant.click ( );
example:
$('.foo').click(function () {
// do stuff here
});
March 12, 2013 at 5:42 pm #127959JohnMotylJr
ParticipantFrom jQuery: Event Performance
March 12, 2013 at 6:09 pm #127963CrocoDillon
ParticipantAh 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 #128415shamai
Memberyeah 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….’
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.