How does $(this.hash) work?

41,555

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".

Share:
41,555
Admin
Author by

Admin

Updated on December 17, 2020

Comments

  • Admin
    Admin over 3 years

    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()
    });