I want to call the following in my code somewhere.
scrollIntoViewElement(main-nav);
The following is the function:
// Element to scroll into view
scrollIntoViewElement(element) {
document.getElementById('element').scrollIntoView();
}
The function should scroll the page into the view of the provided element id.
Is this correct? Or should it be...
// Element to scroll into view
scrollIntoViewElement(element) {
document.getElementById("\'" + element + "\'").scrollIntoView();
}
The code
scrollIntoViewElement(main-nav);means "Take the value ofmain, subtract the value ofnav, and then callscrollIntoViewElementpassing the result in as a parameter. If you want to pass in the stringmain-nav, you need quotes to tell the JavaScript parser you're using a literal string:scrollIntoViewElement("main-nav");Within the function, you don't use quotes, because you want to use the value of the parameter, not the literal string
"element".So:
and
This assumes you have an element with
id="main-nav"in your document. It'll throw an exception if you don't, becausegetElementByIdwill returnnull.