title: How to trigger facebook login onClick
body: <p>How can I trigger Facebook login when a checkbox is checked?</p>
  
  <p>I'm implementing a share to multiple networks feature for my website. I need a bit of help with implementing the facebook login process. As a prerequisite to sharing on my user's behalf I need to make sure the user is logged into facebook and allowed access to my app. Normally this is done using the standard facebook login process.</p>
  
  <p><a href="https://developers.facebook.com/docs/javascript/gettingstarted/" rel="nofollow noreferrer">https://developers.facebook.com/docs/javascript/gettingstarted/</a></p>
  
  <p>I have implemented this and is working fine. I would like to modify the process to display the facebook login page if the following conditions are met:</p>
  
  <p>A. The user checked my "Share with facebook" checkbox AND
  B. The user is not logged in OR
  C. The user has not authorized my app to post on their behalf</p>
  
  <p>Here's what my form looks like:</p>
  
  <pre><code>&lt;form action="post.php" method="post"&gt;
    &lt;input type="checkbox" name="facebook"  value="1" onClick="FB.Login();"/&gt;Facebook
    &lt;label for="Message"&gt;Message&lt;/label&gt;
    &lt;input name="message" type="text"/&gt;
    &lt;input  type="submit" value="post"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here's what my scripts look like (straight from the facebook <a href="https://developers.facebook.com/docs/javascript/gettingstarted/" rel="nofollow noreferrer">tutorial</a>):</p>
  
  <pre><code>&lt;script&gt;
      window.fbAsyncInit = function() {
          FB.init({
              appId      : 'MY APP ID',
              channelUrl : 'my channel url',
              status     : true,
              cookie     : true,
              xfbml      : true
          });
  
          FB.Event.subscribe('auth.authResponseChange', function(response) {
              if (response.status === 'connected') {
                  testAPI();
              } else if (response.status === 'not_authorized') {
                  FB.login();
              } else {
                  FB.login();
              }
          });
  
      };
  
      (function(d){
          var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
          if (d.getElementById(id)) {return;}
          js = d.createElement('script'); js.id = id; js.async = true;
          js.src = "//connect.facebook.net/en_US/all.js";
          ref.parentNode.insertBefore(js, ref);
      }(document));
  
      function testAPI() {
          console.log('Welcome!  Fetching your information.... ');
          FB.api('/me', function(response) {
              console.log('Good to see you, ' + response.name + '.');
          });
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>How can I modify this javascript to popup the facebook login when the checkbox is checked and the user is not yet logged in?</p>
  
  <p>I already tried <a href="https://stackoverflow.com/questions/13508137/facebook-login-onclick-javascript">this</a> suggestion, but it didn't seem to work.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Looks like I made this more complicated than it has to be. The following solution seems to work well. Hope it helps someone else.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  // Initialize the Facebook JavaScript SDK
  FB.init({
    appId: 'APP ID',
    channelUrl : 'channel url',
    xfbml: true,
    status: true,
    cookie: true,
  });
  
  &lt;/script&gt;
  
  &lt;input type="checkbox" name="facebook" value="1" onclick="FB.login();"&gt;
  </code></pre>