Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  53] [ 3]  / answers: 1 / hits: 23773  / 9 Years ago, sun, march 1, 2015, 12:00:00

Very simple onclick not firing.



creating divs in a loop containing the classes:



class=person glyphicon glyphicon-user


and i've tried selecting the class the only way i know



$('.person')click(function(){
console.log(something;
});


it just won't work, i can however print them all with the same selector, and i can fire onclick events on



$('body div').click


so its a div with class person, which leaves me clueless



thanks in advance.



edit: no browser errors, no action, nothing.
the method copy pasted from my code:



$('.person').click(function() {
console.log(something);
});

More From » jquery

 Answers
5

You have multiple possible issues ( and assuming those are only typo's in your question):




  1. Ensure you have jQuery added to your html file and all other jQuery usage comes after that link.


  2. Ensure your JS is wrapped by a $(document).ready(function(){});


  3. creating divs in a loop containing the classes: This leads to believe you are creating them via a JS loop? If it is then you need to call your $('.person').click(function(){}); after the loop. Creating an event on an element type ( class in this case ) does not apply to future elements.



[#67613] Friday, February 27, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;