Search code examples
javascriptjqueryajaxfetchone-liner

Can I replace a jQuery.load() one-liner with an equivalent fetch() call?


I have a page that uses the jQuery one liner

$('#id').load('/url');

to load a fragment into the DOM at a specific place.

If I want to remove the dependency on jQuery, is there an easy alternative way to do this — possibly with the relatively new fetch() API?


Solution

  • Do it like so:

    fetch('/url')
      .then(response => response.text())
      .then(value => {
        document.getElementById('id').innerHTML = value
      });