1

I have two empty anchor links <a href="#"> that are used to create tabs. Please take a look at my jsFiddle:

https://jsfiddle.net/MrSnrub/cuuy5cbp/

The problem is that the user goes to the very top of the page when going from tab to tab. I would use jQuery-UI's tabs, but I have the same HTML element for each tab, and think jQuery-UI's tabs force you to have different HTML elements in each tab.

I want to use these tabs further down my page, and making the user scroll down after every tab switch is not user-friendly. How do I fix this?

UPDATE: If I click on "Tab 1" to activate it, I shouldn't be able to hit the Tab key on my keyboard and Tab 2 is now highlighted.

MrSnrub
  • 435
  • 4
  • 14
  • You can fix this in the jquery functions as well.. but you haven't included any jquery in your question. – Scott Aug 24 '16 at 00:08

3 Answers3

2

You can use href="#/". It should prevent this behavior from happening.

Gefn
  • 141
  • 6
  • I'd like to know that too – Roko C. Buljan Aug 24 '16 at 00:10
  • It prevents the behavior from happening, but is a hack conceptually identical to using `href="#anyIDThatDoestExistOnThePage"`. A better approach would be to _prevent_ the anchor tag from trying to act as a hyperlink in the first place, rather than turning it into an _explicitly broken_ hyperlink – Hamms Aug 24 '16 at 00:12
  • @Hamms `/` is not a valid ID character so I don't think it's the *same* as like you described `href="#anyIDThatDoestExistOnThePage"` but rather a nice URI hack. – Roko C. Buljan Aug 24 '16 at 00:15
  • Well I'm curious to see in what using `href="javascript:void(0)"` is "less hacky" than this answer, since `void(0)` evaluates to undefined, so to me they're kind of equivalent. Actual question. – Gefn Aug 24 '16 at 00:47
1

Use <a href="javascript:void(0)">.

https://jsfiddle.net/obybyvds/

Here's an explanation of why that works

Community
  • 1
  • 1
Rahat Ahmed
  • 2,191
  • 2
  • 30
  • 40
1

You could set the href to javascript:;

<a href="javascript:;">Tab 1</a>

Or keep the href="#" and simply event.preventDefault() on click using this JavaScript:

[].forEach.call(document.querySelectorAll("[href='#']"), function(el) {
  el.addEventListener("click", function(e){
    e.preventDefault();
  }, false);
});

jsFiddle

or in jQuery:

$(document).on("click", "[href='#']", function(e) {
  e.preventDefault();
});
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313