Reputation:
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
Reputation: 9060
Here try this :
search.on("click", function (e) {
e.preventDefault();
$(".hotels").find("div").hide();
var target = $(this).attr('href');
$('#'+target).fadeIn();
});
Upvotes: 0
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();
});
Upvotes: 5
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