user4394417
user4394417

Reputation:

Jquery find id beside href

How can I make with jQuery something like this:

1) If I click to link, it fadeIn div element with same id just like href of clicked link.

My script is here

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

search.on("click", function (e) {
    $(this.hash).hotels("#hash").show();
    e.preventDefault();
});

Whole snipet here http://codepen.io/anon/pen/MwNEgr

Upvotes: 0

Views: 108

Answers (3)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

Here try this :

search.on("click", function (e) {
 e.preventDefault();
 $(".hotels").find("div").hide();
 var target = $(this).attr('href');
 $('#'+target).fadeIn();

});

DEMO

Upvotes: 0

Rick Hitchcock
Rick Hitchcock

Reputation: 35670

Change your anchors as follows:

<a href="#flight">Flights</a>
<a href="#hotel">Hotels</a>
<a href="#cars">Cars</a>

Otherwise, the hash will include the URL.

You can then use this method, and note that you no longer need preventDefault():

search.on('click', function () {
  hotels.hide();
  $(this.hash).fadeIn();
});

Updated CodePen

Upvotes: 5

Zoran P.
Zoran P.

Reputation: 880

search.on("click", function (e) {
   var id = "#" + $(this).attr("href");
    $(id).show();
});

Or you can put "#" in href itself ("#hotels") and then it's

var id = $(this).attr("href");

Upvotes: 0

Related Questions