I have a problem with jquery 1.9.1 . I have searched it but these are not solved my problem.
$('.sm2_expander').live('click', function() {
$(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
return false;
});
Everybody said that "use 'on' function" but this time my code never work.
$(document).on("click", "a.offsite", function(){ alert("Goodbye!"); });
Edit : Here is my using prject page : draggable link
jQuery | live() MethodThis method is used to attach one or more event handlers for the selected elements. It also specifies the function that runs when the event occurs. The event handlers used will work for both current and future elements matching the selector.
This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.
$ is not a function WordPress error occurs when the code comes before the jQuery library. For example, if a plugin or theme calls a code before calling the right library, you get this error. By default, WordPress doesn't understand $ as jQuery and you have to make some modifications to fix this error.
on if not a function" jQuery error occurs for 2 main reasons: Loading an old version of the jQuery library that doesn't support the on function. Loading a library that overrides the value of the dollar sign $ variable.
.live() was introduced in jQuery 1.3, therefore it won't work with earlier versions.
.live() has also since been deprecated in jQuery 1.7 onwards.
The alternatives are .on() and .delegate()
See related question jQuery 1.9 .live() is not a function on how to migrate existing code.
I used "jquery-1.8.3.min.js" and my problem solved.
In your example you have used the selector a.offsite
but there are no elements matching this selector in your page. That might be the reason why it is not working.
$(function(){
$(document).on('click', '.sm2_expander', function(){
alert('bye');
$(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
})
})
I think you can shorten this to
$(function(){
$(document).on('click', '.sm2_expander', function(){
$(this).closest('li').toggleClass('sm2_liOpen sm2_liClosed');
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With