0

I try to get an hash to put filter on my page, I work in JS vanilla only, actually i can't find how getting it.. It must be simple but i can't find on internet and I'm newbie.. If someone can help me, it can be a huge move for me, thank's a lot !

function lien(obj) {
  alert(obj.href.hash);
}
<a href="#portrait" onclick="lien(this)" class="filters" value="portrait">#portrait</a>
lissettdm
  • 12,267
  • 1
  • 18
  • 39
Stampi
  • 47
  • 6
  • Does it have to be hash? `class="filters portrait"` is a little less of a kludge method. Normally you add the `#` to `href` to ensure that a link field doesnt reload the page. – wahwahwah May 17 '21 at 14:45

1 Answers1

2

The href property

Is a USVString that is the result of parsing the href HTML attribute relative to the document, containing a valid URL of a linked resource.

Strings don't have hash properties. You want to read hash directly on the link element itself.

function lien(obj) {
  alert(obj.hash);
}
<a href="#portrait" onclick="lien(this)" class="filters" value="portrait">#portrait</a>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335