1

I am having a problem with my code:

b1 = document.getElementById('headerVv');
var playlistId = 'id';
document.addEventListener("DOMContentLoaded", doSomething)
b1.addEventListener('click', function() {
  playlistId = 'id2';
  doSomething(playlistId)
});

Function doSomething is generating the data on my page. The general thought was to fire up the page with the default playlistId, but when I trigger the eventListener to replace the current data with new playlistId. Swapping the playlistId works, but it's adding new data to the existing data instead of replacing it. Anyone could help me fix it?

5
  • "The general thought was to fire up the page with the default playlistId, but when I trigger the eventListener to replace the current data with new playlistId" <- I can't understand Commented Apr 4, 2020 at 5:23
  • Code of doSomething ? Commented Apr 4, 2020 at 5:24
  • Can you post a working code so that we could help you fix the issue? Without knowing what doSomething is doing, we can't help here Commented Apr 4, 2020 at 5:28
  • doSomething generates videos from youtube API based on the playlistId, When I open the page I want the function to run with the default playlistId witch is id, but when I trigger eventListener I want to run the function with new playlistId, but the data from the initial run of doSomothing stays. Commented Apr 4, 2020 at 5:28
  • Edit your question and post the code snippet of doSomething Commented Apr 4, 2020 at 5:28

1 Answer 1

1

You need to clear the contents before appending the new ones:

function resultsLoop(data) {
  $('main').empty();
  ...

Also consider naming your functions/variables properly, this will be rewarded in the long term.

Sign up to request clarification or add additional context in comments.

1 Comment

have you tested it on my codepen? I have added that, but still the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.