if I would convert my jQuery code into Javascript version. I've two pages: page.html and side.html The page.html take the div id and content from side.html document. It works very fine, but I prefer don't use jQuery for my project. How I can modify my code to native Javascript?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Useless</title>
<script type='text/javascript' src='jquery-3.1.1.min.js'></script>
</head>
<body>
Hello
<script type="text/javascript">
jQuery.ajax({
url: "side.html",
success: function(result) {
var html = jQuery('<div>').html(result);
alert(html.find("div#a").attr("id"));
alert(html.find("div#a").html());
alert(html.find("div#b").attr("id"));
alert(html.find("div#b").html());
},
});
</script>
</body>
</html>
I write this code, but it doesn't work :(
var html = new XMLHttpRequest();
html.open("POST","side.html", true);
success: function(result) {
var html = ('<div>').html(result);
alert(html.find("div#a").attr("id"));
alert(html.find("div#a").html());
alert(html.find("div#b").attr("id"));
alert(html.find("div#b").html());
}
XMLHttpRequest? Also see thefetch()API, which is much nicer.