Mark Bran
asked on
scrollIntoView(); trouble getting it to work
Here is a code pen with my code
https://codepen.io/jsMarko/pen/c115a9b2edc350fe342c9e2b365d6300?editors=1111
Having trouble in my js
Essentially I am using Template Literal to create anchor buttons that when clicked should scroll to the respective section. However, in the test code in above codepen is not working
I just cant seem to see why its not scrolling
Also note the e.preventDefault is not working either as it is still cycling through the sections because of the Anchor href pointing to them
Here is the area I think is the concern const evListen = document.querySelector('#c ontactLink '); // getting contactLink
const evListen = document.querySelector('#c ontactLink '); // getting contactLink
evListen.addEventListener( 'click', (e) => {
e.preventDefault();
let contactEv = document.querySelector('#c ontact');
contactEv.scrollIntoView() ;
console.log('It clicked'); // It clicked is working
});
https://codepen.io/jsMarko/pen/c115a9b2edc350fe342c9e2b365d6300?editors=1111
Having trouble in my js
Essentially I am using Template Literal to create anchor buttons that when clicked should scroll to the respective section. However, in the test code in above codepen is not working
I just cant seem to see why its not scrolling
Also note the e.preventDefault is not working either as it is still cycling through the sections because of the Anchor href pointing to them
Here is the area I think is the concern const evListen = document.querySelector('#c
const evListen = document.querySelector('#c
evListen.addEventListener(
e.preventDefault();
let contactEv = document.querySelector('#c
contactEv.scrollIntoView()
console.log('It clicked'); // It clicked is working
});
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
If you are using jquery I would do something like this
<a id="contactLink" data-link="contact"></a>
$("A[data-link]").on("click" , (function(e) {
var targetData = $(this).attr("data-link");
document.querySelector('#' + targetData).scrollIntoView({block: 'start', behavior: 'smooth'}
});
ASKER
Dont know jquery but that looked interesting
That said adding the onclick to the link is doable
I am creating the links using a Template Literal
let myEl = ['home', 'services', 'team', 'contact'];
// ******* Parse the Array and get all ID names *********
// Use Template Literal to create li then place it in UL
let output = myEl.map((id) => {
return `
<li><a href='#${id}' id='${id}Link'>${id}</a></ li>
`;
});
// ******* Used .join('') on output to remove the trailing commas
document.getElementById('m ylist').in nerHTML = output.join('');
I can adjust the template to include the onclick as you merntioned ... see how that works :) Ill let you know
That said adding the onclick to the link is doable
I am creating the links using a Template Literal
let myEl = ['home', 'services', 'team', 'contact'];
// ******* Parse the Array and get all ID names *********
// Use Template Literal to create li then place it in UL
let output = myEl.map((id) => {
return `
<li><a href='#${id}' id='${id}Link'>${id}</a></
`;
});
// ******* Used .join('') on output to remove the trailing commas
document.getElementById('m
I can adjust the template to include the onclick as you merntioned ... see how that works :) Ill let you know
ASKER
Ok this not turning out as easy as I thought here is whats happening
let output = myEl.map((id) => {
return `
<li><a id='${id}Link' onClick='document.querySel ector('#${ id}').scro llIntoView ({block: 'start', behavior: 'smooth'}); return false;' >${id}</a></li>
`;
});
The results are
<a id="homeLink" onclick="document.querySel ector(" #home').scrollintoview({bl ock:="" 'start',="" behavior:="" 'smooth'});="" return="" false;'="">home</a>
As you can see its adding ="" <----- so ofcourse its not working .... never had Template Literal do that before ..... any thoughts what I could do to fix it?
let output = myEl.map((id) => {
return `
<li><a id='${id}Link' onClick='document.querySel
`;
});
The results are
<a id="homeLink" onclick="document.querySel
As you can see its adding ="" <----- so ofcourse its not working .... never had Template Literal do that before ..... any thoughts what I could do to fix it?
ASKER
forgot to mention M getting this error
SyntaxError: expected expression, got '}'
Im not seeing it though
SyntaxError: expected expression, got '}'
Im not seeing it though
ASKER
Thank you that was the answer ... I figured out how to create the link with the onClick as you showed it ... works like a charm
I used template literal
let output = myEl.map(({id}) => {
return `
<li><a id='${id}Link' onClick="document.querySel ector('#${ id}').scro llIntoView ({block:'s tart',beha vior:'smoo th'});retu rn false;" >${id}</a></li>
`;
});
got it done ..... Thanx
I used template literal
let output = myEl.map(({id}) => {
return `
<li><a id='${id}Link' onClick="document.querySel
`;
});
got it done ..... Thanx
Based on the lack of animation in the original question, may I ask why not just
document.getElementById('mylist').innerHTML = myEl.map(id => `<li><a id='${id}Link' href='#${id}'>${id}</a></li>`).join('');
or even without the ID if you do not need itdocument.getElementById('mylist').innerHTML = myEl.map(id => `<li><a href='#${id}'>${id}</a></li>`).join('');
ASKER
const homeLink = document.getElementById('h
homeLink.onclick = function(e) {
e.preventDefault();
document.querySelector('#h
}
const servLink = document.getElementById('s
servLink.onclick = function(e) {
e.preventDefault();
document.querySelector('#s
}
const teamLink = document.getElementById('t
teamLink.onclick = function(e) {
e.preventDefault();
document.querySelector('#t
}
const conLink = document.getElementById('c
conLink.onclick = function(e) {
e.preventDefault();
document.querySelector('#c
}
Instead of repeating the functions over and over how
might I write one function that could be called by the anchor onclick events?