mardi 18 décembre 2018

Javascript function - dynamicly replace .innerHTML within [duplicate]

This question already has an answer here:

I am trying to optimize some code, so I can dynamicly make it get HTML content from the Website. So I got the following code, and would like to sometimes use .innerHTML sometimes .href as replacement, while keeping .innerHTML as the default value.

function copySourceToDestination(the_source, the_destination) {
  var featureSource       = document.querySelector(the_source).innerHTML;
  var featureDestination  = document.querySelector(the_destination);

  featureDestination.innerHTML = featureSource;
}

However if i try this it doesn't work:

function copySourceToDestination(the_source, the_destination, the_selector = 'innerHTML') {
  var featureSource       = document.querySelector(the_source).the_selector;
  var featureDestination  = document.querySelector(the_destination);

  featureDestination.the_selector = featureSource;
}




Aucun commentaire:

Enregistrer un commentaire