Sasha Ue Chirico Sasha Ue Chirico - 4 months ago 6
HTML Question

Why doesn't work my jQuery selector "this" with ID anchors?

I have a texts with tag

<a>
, if you click on them they should take you on the next anchor, with a smooth scroll.

There is the code:

HTML

<div style="width:100%;height:1500px;background-color:black;color:white;">ciao
<div>
<a class="scrolldown" style="color:white" href="#prova">ciaoooooo</a>
</div>
</div>

<div id="prova" style="width:100%;height:1500px;background-color:silver;color:white;">ciao ancora
<div>
<a class="scrolldown" style="color:white" href="#prova2">ciaoooooo</a>
</div>
</div>

<div id="prova2" style="width:100%;height:1500px;background-color:green;color:white;">basta
</div>


JavaScript

$(".scrolldown").click(function() {
var cane = $('this').attr("href");
$('html,body').animate({
scrollTop: $(cane).offset().top},
'slow');
});


FIDDLE
http://jsfiddle.net/mar1/yLroumv6/

Answer

$('this') will select <this> element in the DOM. Use $(this).

Updated Fiddle

Comments