KraigBalla
KraigBalla

Reputation: 363

Create link to specific jQuery tab

I've looked around and tried several things from similar post about jQuery tabs, but I can't get my code to work the same. Below is what I'm working with. Goal is to have the link open the correct tab.

Link to the tab:

 <li><a onClick="$('.tabs').selectorr('.about', 250).click()" href="#">About Us</a></li>

The code for my tabs:

<ul class="tabs">
            <li>
                <h3>Games</h3>
                <div class="split">
                        <p>Current Games</p>
                        <div>Tab 1 content</div>                        
                </div>
            </li>
            <li>
                <h3 class="about">Who We Are</h3>
                <div class="split reversed">
                        <div>Tab 2 content</div>
                </div>
            </li>
</ul>

My main.js to call the tab: this is called when the page initially loads, which selects the first h3

    $('.tabs').selectorr({
        titleSelector: 'h3',
        delay: 250
    });

The selectorr function:

/* jquery.selectorr v0.2 | (c) @ajlkn | MIT licensed */
!function(e){
    e.fn.selectorr=function(t){
        var i=e(this),s=e.extend({
            titleSelector:".title",delay:0
        },t);
        if(0==this.length)
            return i;
        if(this.length>1){
            for(var a=0;a<this.length;a++)e.selectorr(e(this[a]),s);
            return i
        }
        return e.selectorr(i,s)
    },e.selectorr=function(t,i){
        var s,a,l,n=t.children("li"),r=e('<div><ul class="titles"></ul><div class="panels"></div></div>').insertAfter(t),c=r.find(".titles"),d=r.find(".panels"),o=0,v=!1;(l=n.filter(".active")).length>0&&(o=l.index()),n.each(function(t){
            var l,n,r,f,u=e(this);
            r=u.find(i.titleSelector),f=r.attr("href"),l=e('<li class="title">'+r.html()+"</li>"),c.append(l),r.remove(),l.css("cursor","pointer"),u.wrapInner('<div class="panel" />'),n=u.children("div"),n.appendTo(d),l.on("click",function(e){
                if(v||l.hasClass("active"))return!1;
                if(f)return location.href=f,!1;v=!0,s.removeClass("active"),l.addClass("active");
                var t=a.filter(".active");
                return t.length>0?(t.removeClass("active"),setTimeout(function(){
                    t.css("display","none"),n.show(),n.addClass("active"),setTimeout(function(){
                        v=!1},i.delay)
                },i.delay)):(n.css("display",""),n.addClass("active"),setTimeout(function(){
                    v=!1
                },i.delay)),!1
            }),t==o?(l.addClass("active"),n.addClass("active")):n.css("display","none")
        }),r.attr("id",t.attr("id")).attr("class",t.attr("class")),t.remove(),s=c.find(".title"),a=d.find(".panel")
    }
}(jQuery);

Upvotes: 1

Views: 88

Answers (1)

Swati
Swati

Reputation: 28522

Your plugin replace previous html and add new htmls i.e : <li class="title">..</li> so .about doesn't exist in your dom . Instead you can use '$(".titles li:eq(1)").trigger("click")' to trigger second li i.e : about tab .

Demo Code :

/* jquery.selectorr v0.2 | (c) @ajlkn | MIT licensed */ ! function(e) {
  e.fn.selectorr = function(t) {
    var i = e(this),
      s = e.extend({
        titleSelector: ".title",
        delay: 0
      }, t);
    if (0 == this.length)
      return i;
    if (this.length > 1) {
      for (var a = 0; a < this.length; a++) e.selectorr(e(this[a]), s);
      return i
    }
    return e.selectorr(i, s)
  }, e.selectorr = function(t, i) {
    var s, a, l, n = t.children("li"),
      r = e('<div><ul class="titles"></ul><div class="panels"></div></div>').insertAfter(t),
      c = r.find(".titles"),
      d = r.find(".panels"),
      o = 0,
      v = !1;
    (l = n.filter(".active")).length > 0 && (o = l.index()), n.each(function(t) {
      var l, n, r, f, u = e(this);
      r = u.find(i.titleSelector), f = r.attr("href"), l = e('<li class="title">' + r.html() + "</li>"), c.append(l), r.remove(), l.css("cursor", "pointer"), u.wrapInner('<div class="panel" />'), n = u.children("div"), n.appendTo(d), l.on("click", function(e) {
        if (v || l.hasClass("active")) return !1;
        if (f) return location.href = f, !1;
        v = !0, s.removeClass("active"), l.addClass("active");
        var t = a.filter(".active");
        return t.length > 0 ? (t.removeClass("active"), setTimeout(function() {
          t.css("display", "none"), n.show(), n.addClass("active"), setTimeout(function() {
            v = !1
          }, i.delay)
        }, i.delay)) : (n.css("display", ""), n.addClass("active"), setTimeout(function() {
          v = !1
        }, i.delay)), !1
      }), t == o ? (l.addClass("active"), n.addClass("active")) : n.css("display", "none")
    }), r.attr("id", t.attr("id")).attr("class", t.attr("class")), t.remove(), s = c.find(".title"), a = d.find(".panel")
  }
}(jQuery);


$('.tabs').selectorr({
  titleSelector: 'h3',
  delay: 250
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="tabs">
  <li>
    <h3>Games</h3>
    <div class="split">
      <p>Current Games</p>
      <div>Tab 1 content</div>
    </div>
  </li>
  <li>
    <h3 class="about">Who We Are</h3>
    <div class="split reversed">
      <div>Tab 2 content</div>
    </div>
  </li>
</ul>
<!--click on second li-->
<a onclick='$(".titles li:eq(1)").trigger("click")' href="#">About Us</a>

Upvotes: 1

Related Questions