I am trying to change an arbitrary paragraph to the content it from an external file. Here it is:
External Files/text.txt
:
jQuery and AJAX do go well together!
Here is the file that helps change the paragraph:
jquery_ajax1.html
:
...
<script language="Javascript">
...
$('button').click(function() {
$('#p1').load('External Files/text.txt', function(responseText, statusTxt, xhr) {
if(statusTxt == 'success') {
window.alert("External content loaded successfully!");
}
if(statusTxt == 'error') {
window.alert("You've got problems." + "\n" + xhr.status + ": " + xhr.statusText);
}
});
});
</script>
<body>
...
<p id="p1">Click the below button to change me!</p>
<button>Click to change paragraph using jQuery and AJAX!</button>
</body>
When I run the program, I get the parapgraph and the button, but when I click the button, the paragraph doesn't change. I tried wrapping the paragraph inside of a <div></div>
, but nothing worked. I originally tried adding a new paragraph and appending the file to the new paragraph. I also tried commenting out the paragraph. Nothing worked. Is there something wrong with my code? How can I fix this?
Notes:
I saw this question, this question, this question, this question, and a lot more, but they didn't help me solve my problem.