2

I know this is a little bit hard to understand, but I've seen some sites using the ID of an element in the url to send you somewhere, and I was wondering if it's also possible with classes.

I don't think this is private or something like that because anyone can get it by inspecting the page. I also tryed with a dot (".class"), but it unfortunately didn't work.

So is there another way of doing this with other than IDs?

  • clicking on a link with a hashtag will scroll you to the point on that page where that id occurs. it's called a bookmark. if you used classes how would the browser know which one to scroll to? why do you want to do that anyway? – Robert Parham Aug 30 '16 at 20:29
  • Hashes can only refer to `id` attributes and `name` attributes, but not `class` or any other attribute. – mwcz Aug 30 '16 at 20:29
  • 1
    Wrong answer Arnav. But anyway, I don't think there's anything that can be done with classes as there is no guarantee a class is unique, therefore how does the browser know which to scroll to? Could implement some behavour with JavaScript which takes the first – Joseph Young Aug 30 '16 at 20:29

1 Answers1

8

You cannot do this with classes because they're not unique. The browser needs to know exactly where to send you, and using a class name instead of an ID would be ambiguous.

Lazar Ljubenović
  • 18,976
  • 10
  • 56
  • 91