title: JQuery selectors within if statements
body: <p>As for context, in my page when i click on a tag it filters out and shows the posts corresponding to the clicked tag, from here i need to handle pagination to go to the next bunch of posts.</p>
  
  <p>But I'm struggling with the javascript <code>if</code> statement here with jQuery, where i don't manage to differentiate the click on tag and the click on the next button to go to next page.</p>
  
  <p>On top of that whereas i click on the tag or the next button i need to always grab the same variables <code>$clickTag</code>,<code>$catName</code>,<code>$page</code> </p>
  
  <p><code>$clickTag</code> value changes only if I click on the tag Name and</p>
  
  <p><code>$page</code> value changes only if i click on the next button</p>
  
  <p>In my current situation the first part is working properly <code>if</code> when clicking on a tag but the second part <code>else</code> doesn't, i want to grab the page number, but instead it grab the text and store it in the <code>$clickTag</code> variable…</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>(function($) {
    $doc = $(document);
  
    $doc.ready(function() {
      var $container = $('#main');
      var $pagePosts = $container.find('article');
      var $postNav = $('nav');
      var $status = $('.ajax_tag');
      var $catName = "";
      var $name = "";
      var $page = "1";
      var $clickTag = "";
  
      $("a[rel='tag']").addClass("tag");
      $pagePosts.wrapAll("&lt;span class='my_container'&gt;&lt;/span&gt;");
  
      var $myContainer = $('.my_container');
  
      $container.on('click', "a[rel='tag'], .pagination a", function(tag) {
        event.preventDefault();
        if ($('.tag')) {
          $clickTag = $(tag.target).text();
          getCatName($name);
          $page = "1";
        } else {
          $clickTag = $(tag.target).text();
          getCatName($name);
          $page = parseInt($this.attr('href').replace(/\D/g, ''));
        }
  
        $params = {
          'tag': $clickTag,
          'cat': $catName,
          'page': $page,
        };
  
        get_posts($params);
      });
    });
  })(jQuery);</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div id="primary" class="content-area"&gt;
    &lt;main id="main" class="site-main" role="main"&gt;
      &lt;span class="my_container"&gt;
        &lt;article id="post-4996" class="post-4996 post type-post status-publish format-standard hentry category-mdu_news tag-dog"&gt;
          &lt;header class="entry-header"&gt;MDU_TEST  
            &lt;a href="http://192.168.33.10/mdu_test/mdu_news/4996" rel="bookmark"&gt;MDU – allez un petit dernier pour la route&lt;/a&gt;
            &lt;div id="ajax_tag"&gt;TAGS:&lt;a href="http://192.168.33.10/tag/dog" rel="tag" class="tag"&gt;dog&lt;/a&gt;
              &lt;div class="MDU_TEST"&gt;&lt;/div&gt;
            &lt;/div&gt;
          &lt;/header&gt;
        &lt;/article&gt;
        &lt;article id="post-4994" class="post-4994 post type-post status-publish format-standard hentry category-mdu_news tag-cat tag-dog"&gt;
          &lt;header class="entry-header"&gt;MDU_TEST  
            &lt;a href="http://192.168.33.10/mdu_test/mdu_news/4994" rel="bookmark"&gt;MDU – vu que j’ai pas mal de tags je dois faire beaucoup de posts…&lt;/a&gt;
            &lt;div id="ajax_tag"&gt;TAGS:&lt;a href="http://192.168.33.10/tag/cat" rel="tag" class="tag"&gt;cat&lt;/a&gt;, &lt;a href="http://192.168.33.10/tag/dog" rel="tag" class="tag"&gt;dog&lt;/a&gt;
              &lt;div class="MDU_TEST"&gt;&lt;/div&gt;
            &lt;/div&gt;
          &lt;/header&gt;
        &lt;/article&gt;
      &lt;/span&gt;
      &lt;nav class="navigation pagination" role="navigation"&gt;
        &lt;h2 class="screen-reader-text"&gt;Posts navigation&lt;/h2&gt;
        &lt;div class="nav-links"&gt;
          &lt;span class="page-numbers current"&gt;
            &lt;span class="meta-nav screen-reader-text"&gt;page &lt;/span&gt;1
          &lt;/span&gt;
          &lt;a class="page-numbers" href="http://192.168.33.10/category/mdu_test/page/2"&gt;
            &lt;span class="meta-nav screen-reader-text"&gt;page &lt;/span&gt;2&lt;/a&gt;
          &lt;a class="page-numbers" href="http://192.168.33.10/category/mdu_test/page/3"&gt;&lt;span class="meta-nav screen-reader-text"&gt;page &lt;/span&gt;3&lt;/a&gt;
          &lt;a class="next page-numbers" href="http://192.168.33.10/category/mdu_test/page/2"&gt;next page&lt;/a&gt;
        &lt;/div&gt;
      &lt;/nav&gt;
    &lt;/main&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>