0

I've voting mechanism in my website, if user tries to vote(up or down) I'll check whether he logged-in or not, for that I've written following code,

      $(".vote").click(function(){
         if(is_logged_in)
          {
             // Doing necessary stuff for vote up/down
          }else
            {
             // Showing Login Dialog box                        
            submitForm(".lform",function(response){

             if(data.status == "1")
               {
                 //Closing the Login Dialog Box
                 is_logged_in = 1;  // Assigning the value
                 bindEvent(".vote","click");  // Binding the Event,but can't able to it
               }
              });//Form Submission Closed
            }// Else Closed
         });// Main 'click' closed

     function bindEvent(selector,eventType){
         $(selector).bind(eventType);
        }

Instead of external function bindEvent(".vote","click") I've tried without the bindEvent(), but I can't able to bind the click event dynamically after successful login.

0

2 Answers 2

1

In order to bind something to occur on an event you need to pass a callback to be triggered when that event occurs. In your code above you aren't specifying a callback.

function bindEvent(selector,eventType){
  $(selector).bind(eventType);
}

You need something like this:

function bindEvent(selector,eventType,callBack){
  $(selector).bind(eventType, callBack);
}

Which you would use like:

bindEvent('.target', 'click', function(){
  alert('This will trigger on click!');
})

update

After re-reading your code it looks to me like what you actually need is to trigger the click event, rather than bind something to it:

$(".vote").click(function(){
  var $vote = $(this);
  if(is_logged_in) {
    // Doing necessary stuff for vote up/down
  }
  else {
    // Showing Login Dialog box                        
    submitForm(".lform",function(response){
      if(data.status == "1"){
        //Closing the Login Dialog Box
        is_logged_in = 1;  // Assigning the value
        $vote.click();
      }
    });//Form Submission Closed
  }// Else Closed
});

Using click() is the simple method, or you could use jQuery's .trigger('click') method. You may also wish to avoid the click event bubbling up to parent elements, which in this case would make sense, so you could also use .triggerHandler('click') instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I don't need callback function here, before posting by question I've tried keeping callback also,but not worked for me.
@Mahesh.D binding without a second parameter will not do anything. You can either pass a callback or false. Passing false will prevent the event from triggering it's default behaviour, passing a callback will allow you to trigger something on that event. Other than those two options what are you trying to do? because it sounds like you don't need to use bind.
1

You should use on() for binding dynamically created elements.

 $(document).on("click", ".vote",function(){
      if(is_logged_in)
      {
         // Doing necessary stuff for vote up/down
      }
      else
      {
         // Showing Login Dialog box                        
        submitForm(".lform",function(response){

         if(data.status == "1")
           {
             //Closing the Login Dialog Box
             is_logged_in = 1;  // Assigning the value
             bindEvent(".vote","click");  // Binding the Event,but can't able to it
           }
          });//Form Submission Closed
        }// Else Closed
});// Main 'click' closed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.