Sasha Ue Chirico Sasha Ue Chirico - 1 year ago 43
HTML Question

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

I have a texts with tag

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

There is the code:


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

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

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


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


Answer Source

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

Updated Fiddle