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");
});
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");
});
Share
Improve this question
edited Mar 1, 2015 at 14:18
Kasper Sølvstrøm
asked Mar 1, 2015 at 14:10
Kasper SølvstrømKasper Sølvstrøm
2801 gold badge2 silver badges22 bronze badges
7
|
Show 2 more comments
7 Answers
Reset to default 5You have multiple possible issues ( and assuming those are only typo's in your question):
Ensure you have jQuery added to your html file and all other jQuery usage comes after that link.
Ensure your JS is wrapped by a
$(document).ready(function(){});
"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.
try to use jquery on instead of click
$( ".person" ).on( "click", function() {
console.log("something");
});
I am not sure if the element with class "person" is dynamically created. Either way, try:
$("body").on("click", ".person", function() {
console.log("something");
});
If its not solved yet, Try this way too
$(document).on('click','.person',function(){
console.log("something");
});
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.
$(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>
});
I am not sure whether you have done as below. But try this trouble shooting:
First of all, check if you imported jquery.min.js
correctly, 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)
.ready
? – underscore Commented Mar 1, 2015 at 14:11dot
...$('.person').click()
. Use browser console to look for errors thrown – charlietfl Commented Mar 1, 2015 at 14:12