Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does $(this.hash) work?

Tags:

jquery

hash

How $(this.hash) works in jQuery? I presupposed that this script should work like this - if I click to link with href tickets it will show div with id tickets. But it not works.

var search = $("#switcher").find("a"),
    hotels = $("#find").children("div").hide();

search.on('click', function (e) {

  $(this.hash).show()
  e.preventDefault()
});

1 Answers

this.hash reads the href attribute of this, and gets the part of the URL beginning with #. So if the anchor looks like:

<a href="someURL#foobar">

this.hash will be #foobar. When you then use $(this.hash).show(), it's equivalent to doing $("#foobar").show(), so it will show the element with id="foobar".

like image 113
Barmar Avatar answered Sep 08 '25 10:09

Barmar