I'm trying to trigger existing jQuery function with click. I been told that this can be done by .bind: http://api.jquery.com/bind/
I'm using below code to swipe trought images:
a) swipe left = I don't like it
b) swipe right = I like it
I want to be able to do exactly the same with clicking on the links.
*.js code:
jQuery(function($){
var likedCount = 0;
$('.postswiper-post').live('swiperight',function(){
if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
$(this).addClass('rot-left');
$('.postswiper-post').find('.status').remove();
$(this).append('<div class="status like">Like!</div>');
likedCount++;
var postTitle = $(this).find('.postswiper-posttitle').html();
$('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
$('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');
if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
$('.postswiper-likedlist-opener').removeClass('disabled');
}
if ($(this).is(':last-child')) {
$('.postswiper-wrapper > p').css('color','inherit');
}
}
});
//
jQuery: https://gist.github.com/edoardoo/89e672f22fa405799fdd
/*! jQuery Mobile v1.4.5 | Copyright 2010, 2014 jQuery Foundation, Inc. | jquery.org/license */
I have added following html code to my page:
<div class="colnew"><a class="click-right" href="/" onclick="return false;">LOVE IT!</a></div>
and to *.js file:
jQuery(function($){
var likedCount = 0;
$('.postswiper-post').live('swiperight',function(){
if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
$(this).addClass('rot-left');
$('.postswiper-post').find('.status').remove();
$(this).append('<div class="status like">Like!</div>');
likedCount++;
var postTitle = $(this).find('.postswiper-posttitle').html();
$('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
$('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');
if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
$('.postswiper-likedlist-opener').removeClass('disabled');
}
if ($(this).is(':last-child')) {
$('.postswiper-wrapper > p').css('color','inherit');
}
}
});
//my bind code
function fBindFunctionToElement(){
$('.click-right').bind('swiperight',function(){
if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
$(this).addClass('rot-left');
$('.postswiper-post').find('.status').remove();
$(this).append('<div class="status like">Like!</div>');
likedCount++;
var postTitle = $(this).find('.postswiper-posttitle').html();
$('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
$('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');
if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
$('.postswiper-likedlist-opener').removeClass('disabled');
}
if ($(this).is(':last-child')) {
$('.postswiper-wrapper > p').css('color','inherit');
}
}
});
(...)
When I click on "LOVE IT" link it doesn't trigger the jQuery function.
1. What I'm doing wrong?
2. Is there any other way to call existing jQuery function with click?
Edit:
The solution below partially solves the problem:
$(document).on("click",".click-right",function() {
if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
$(this).addClass('rot-left');
$('.postswiper-post').find('.status').remove();
$(this).append('<div class="status like">Like!</div>');
likedCount++;
var postTitle = $(this).find('.postswiper-posttitle').html();
$('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
$('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');
if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
$('.postswiper-likedlist-opener').removeClass('disabled');
}
if ($(this).is(':last-child')) {
$('.postswiper-wrapper > p').css('color','inherit');
}
}
});
When I'm clicking on "LOVE IT" this part of the code is triggered
$('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');
but I can't get image to reload same as if I was using mouse swipe. I have a feeling that I forgotten something, something very obvious lol