0

I'm using a Wordpress.org website with a JQuery tabs plugin, each page has 10 posts total, each post has it's own JQuery tab set.

So for Post 1 the tabs are labeled #tabs-1-1, #tabs 1-2, etc.

Post 2 the tabs are #tabs-2-1, #tabs-2-2, etc....each tab set has unique id's.

For each post I have a buy now button inside Tab 1 that links to it's Tab 4 to show purchasing options for the product. I'm trying to find out if it's ok to use this code to jump to tab 4:

POST 1:

<a onclick="$('a[href=#tabs-1-4]').click();">BUY NOW</a>

POST 2:

<a onclick="$('a[href=#tabs-2-4]').click();">BUY NOW</a>

POST 3:

<a onclick="$('a[href=#tabs-3-4]').click();">BUY NOW</a>

And so on and so on. The onclick would appear 10 times on each page. I've read that inline javascript is bad which leads me to creating this post as I want to use the best/safest method possible.

I've read the proper way is to put the function in a js file (unobtrusive javaScript) and call it using an id but since each onclick goes to a different tab hash I don't know if that method will work or how to make it work. I have over 400 posts total so would I write 400 different functions in the js file?

Then there's the issue of which link to use if it's ok to use it:

<a onclick="$('a[href=#tabs-1-4]').click();">BUY NOW</a>
<a href="javascript:void(0)" onclick="$('a[href=#tabs-1-4]').click();">BUY NOW</a>
<a href="#" onclick="$('a[href=#tabs-2-4]').click(); return false;">BUY NOW</a>
Chris
  • 5,876
  • 3
  • 43
  • 69
Nate M.
  • 111
  • 2
  • 12
  • Have you tried BUY NOW? Nevermind, just tried it. – Chris Mar 12 '14 at 18:48
  • possible duplicate of [selecting & loading a jquery tab programatically](http://stackoverflow.com/questions/970838/selecting-loading-a-jquery-tab-programatically) – Ярослав Рахматуллин Mar 12 '14 at 18:54
  • That question focuses on the method more than the best syntax for the links themselves. – Chris Mar 12 '14 at 18:55
  • There are lots of good related discussions though, in the sidebar. For example: http://stackoverflow.com/q/3668614/40352 http://stackoverflow.com/q/134845/40352 – Chris Mar 12 '14 at 19:04
  • I have both of those posts in my favorites :) I just started reading about javascript 3 days ago so I'm definitely a noob, just trying to wrap my head around it. My issue is I have so many different tab hashes and each onclick needs to point to a unique tab hash. – Nate M. Mar 12 '14 at 19:09
  • Currently it works fine with the a onclick= link I posted in my original post, I just want to make sure that method isn't going to break all of a sudden or cause my website to crawl. I'm reading your answer now. – Nate M. Mar 12 '14 at 19:12

1 Answers1

1

If you want to be fancy and have html5 support, I would probably make each link similar to this:

<a class="tabLink" data-href="#tabs-1-4">BUY NOW</a>
<a class="tabLink" data-href="#tabs-2-4">BUY NOW</a>

Script (works for all links at once):

var jqTabHost = ".whateverTheNameOfYourTabElementIs";
$('a.tabLink').click(function() {
    //Something like (not sure exactly):
    var href = $(this).data('href');
    //EITHER
    $('a[href=' + href + ']').click();
    //OR (better)
    $(jqTabHost).tabs( "load", href.substring(1) );
    return false;
});

Without custom attribute support you could use href instead of data-href, and munge the href so it's not quite the same.

The idea behind this is keeping the code as short as possible and separating the data (where something links, in HTML) from the behavior (how to make that link work, in script).

Chris
  • 5,876
  • 3
  • 43
  • 69
  • So each tab hash (ie #tabs-1-4) would replace #fragment-1? And for the script, will that one script work for all tab links? I'm a little confused. So would it be: `BUY NOW ` – Nate M. Mar 12 '14 at 20:09
  • Bit like that yeah, let me revise. – Chris Mar 12 '14 at 20:10
  • It's not doing anything when I click the buy now button, probably implemented it wrong: http://testsite.rockitpro.com/2014/02/26/test-beat-w-hook-5/ (js file is here http://testsite.rockitpro.com/wp-content/themes/vantage-child/tablinks.js) – Nate M. Mar 12 '14 at 20:37
  • Hmm. First, I'm not seeing the click handler get triggered, which is a bit odd. Second, you'll have to change ".selector" to whatever your tab element is. I would go with $('a[href=' + href + ']').click(); for now, just as a matter of expediency. – Chris Mar 12 '14 at 23:44
  • I switched to $('a[href=' + href + ']').click(); and tried with data-href (nothing happens) & with just href on the link & it just puts the hash in the url bar, doesn't switch over to the tab. I just realized I cant even use this method because when you go to a post's page, the plugin sets the tab hash back to #tabs-1-1, so if the 2nd post on the blog list view is #tabs-2-1, it'll be #tabs-1-1 on single view. I would need to make a function that can select the 4th tab without an id, I doubt that's even possible. Thanks for the help Chris, back to the drawing board for me. – Nate M. Mar 13 '14 at 02:37
  • If you do experiment more, I would recommend using JSFiddle so you can isolate the parts you're working on. Good luck. – Chris Mar 13 '14 at 16:52
  • 1
    I actually got it working. The plugin was forcing the id names of the tabs so I just deleted the plugin and manually added jquery tabs so I could choose the id names. Works great now. Gonna look at your code again soon, should work fine now that I'm using straight jquery tabs. – Nate M. Mar 14 '14 at 15:53