Answer the question
In order to leave comments, you need to log in
Update div content on button without page reload?
Hello, how can I update the content of a div on a button without reloading the page (just refresh)? I am a beginner, there are many different solutions on the Internet, but there is also a lot of water in these solutions. Tell me please
<button class="test1" id="test" onclick="">Обновить</button>
<div class="soderzhimoye">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
Answer the question
In order to leave comments, you need to log in
An example of a function for selectively updating content:
async function elementUpdate(selector) {
try {
var html = await (await fetch(location.href)).text();
var newdoc = new DOMParser().parseFromString(html, 'text/html');
document.querySelector(selector).outerHTML = newdoc.querySelector(selector).outerHTML;
console.log('Элемент '+selector+' был успешно обновлен');
return true;
} catch(err) {
console.log('При обновлении элемента '+selector+' произошла ошибка:');
console.error(err);
return false;
}
}
elementUpdate('.soderzhimoye');
<button onclick="elementUpdate('.soderzhimoye');">Нажми меня, чтобы обновить блок</button>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question