hi does anyone have any ideas what im doing wrong here?
When you click show content i want it to find the closest div with the class i ask and scroll to it but does not work... im new to jquery so maybe im doing wrong but i think my logic is correct haha
getting an error in console:
Cannot read property 'top' of undefined
Example:
$(document).ready(function() {
// show examples
$(document).on("click", ".show-syntax", function(e) {
var ele = $(this).parent().closest(".render-syntax");
// this will search within the section
ele.show();
$("html, body").animate({
scrollTop: $(ele).offset().top
}, 100);
e.preventDefault();
});
});
.render-syntax {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<a href="#" class="show-syntax">show content</a>
</div>
<div class="render-syntax">
<p>content to show</p>
</div>
<div class="container">
<a href="#" class="show-syntax">show content</a>
</div>
<div class="render-syntax">
<p>content to show</p>
</div>
<div class="container">
<a href="#" class="show-syntax">show content</a>
</div>
<div class="render-syntax">
<p>content to show</p>
</div>