I use a code for loading HTML into a div-container. The goal is to have a single page application. Now I want to use the loaded content and sometimes change texts or designs. At the moment it doesn't work as I want.
One of three parts shows the most important part I think:
Router.prototype = {
routes: undefined,
rootElem: undefined,
constructor: function (routes) {
this.routes = routes;
this.rootElem = document.getElementById('app');
},
init: function () {
var r = this.routes;
(function(scope, r) {
window.addEventListener('hashchange', function (e) {
scope.hasChanged(scope, r);
});
})(this, r);
this.hasChanged(this, r);
},
hasChanged: function(scope, r){
if (window.location.hash.length > 0) {
for (var i = 0, length = r.length; i < length; i++) {
var route = r[i];
if(route.isActiveRoute(window.location.hash.substr(1))) {
scope.goToRoute(route.htmlName);
}
}
} else {
for (var i = 0, length = r.length; i < length; i++) {
var route = r[i];
if(route.default) {
scope.goToRoute(route.htmlName);
}
}
}
},
goToRoute: function (htmlName) {
(function(scope) {
var url = 'views/' + htmlName,
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
scope.rootElem.innerHTML = this.responseText;
}
};
xhttp.open('GET', url, true);
xhttp.send();
})(this);
}
};
At the end you can see xhttp.open('GET'...). Because this is code I took from a tutorial I don't want to mess around with it, but select loaded elements in a separate script. I have a page with a container having the ID 'app'. The files loaded are html like about.php
<div><h1 id="abt">About</h1></div>
<a href="#list">List</a>
Because I want to select them in an extra file I don't like to use a script like the following
$(document).on("load", function() {
ChangeTextFunction();
});
Instead, I want to learn how I can select the elements from the request mentioned above in an extra file, a separate request. E.g. $('#abt').text("Hello")
;
Is it somehow possible? Thanks in advance.