JS Click not working

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");
});

Upvotes: 4

Views: 29557

Answers (7)

Shijo Rs
Shijo Rs

Reputation: 157

$(document).off('click','.person').on('click','.person',function(){ console.log('hello world');

$(document).ready(function(){
$('.person').click(function(){
alert('helloooo...')
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<button class="person">click me</button>

});

Upvotes: 1

Linoy
Linoy

Reputation: 1395

If its not solved yet, Try this way too

$(document).on('click','.person',function(){
   console.log("something");
});

Upvotes: 2

Chintan Soni
Chintan Soni

Reputation: 25267

I am not sure whether you have done as below. But try this trouble shooting:

First of all, check if you imported jquery.min.jscorrectly, which includes correct path to jquery.

Then, check if your script loads only after jquery.min.js is loaded.

Once, again please make sure your code (or script) is fired or called only after jquery.min.js is loaded.

If this is ok, then, check if, in between any script fails to execute, as if any script fails to run in between, the code following it will be skipped. I mean to say like:

line in which your `jquery.min.js`
.
.
.
.
.
Some code that is error prone
.
.
.
.
.
And at last your script lies (which wont fire)

Upvotes: 0

lshettyl
lshettyl

Reputation: 8181

I am not sure if the element with class "person" is dynamically created. Either way, try:

$("body").on("click", ".person", function() {
    console.log("something");
});

Upvotes: 2

kockburn
kockburn

Reputation: 17616

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.

Upvotes: 5

Michel
Michel

Reputation: 28289

You missed a dot after the jquery selector :

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

should be

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

Also, ensure your click handler is declared after the div with class person

Edit, as it seems it is a typo only in your question and not in your code.

Upvotes: 1

Vladu Ionut
Vladu Ionut

Reputation: 8193

try to use jquery on instead of click

$( ".person" ).on( "click", function() {
console.log("something");
});

Upvotes: 2

Related Questions