I am trying to implement a function that scrolls the div to the top position. However, I am encountering an issue with retrieving the href value. When I use 'a' in console.log(a);
, it returns undefined.
function myFunction()
{
var a=$(this).attr('href');
console.log(a);
$('html, body').animate({scrollTop: $('#'+a).offset().top-40}, 500);
}
#consulting,#segments,#partner,#insights{min-height:100vh;}
.secMenu{position:fixed;
}
<div class="container-fluid">
<div class="row secMenu">
<div class="col-md-9 col-sm-12 menu">
<ul class="nav navMenu">
<li class="test1"><a href="#consulting" onclick="myFunction()">Consulting & Solutions</a></li>
<li class="test2"><a href="#segments" onclick="myFunction()">Segments</a></li>
<li class="test3"><a href="#partner" onclick="myFunction()">Our Partners</a></li>
<li class="test4"><a href="#insights" onclick="myFunction()">Perspectives</a></li>
</ul>
</div>
</div> <!--End of second menu -->
<div class="row">
<div id="consulting">
div1
</div>
<div id="segments">
div11
</div>
<div id="partner">
div111
</div>
<div id="insights">
div1111
</div>
</div>
</div>