2

I'm having a problem changing the string content of a particular DIV tag when using AJAX.

For some reason I can change string content when using an onclick function. This works;

<div id="demo">Will change on click? </div>

<button onclick="myFunction()">Try it</button>

<script>
    function myFunction() {
        document.getElementById("demo").innerHTML = "Yes, Successfully changes" ;
    }
</script>

However this does not;

<div id="demo2">Will this change?</div>
<script>
    window.onload = function() {
        document.getElementById("demo2").innerHTML = "Yes, Successfully changes" ;
    }
</script>

Both approaches work on the page itself, but import that page using AJAX, and only the onclick method works. This issue persists when trying both JavaScript and JQuery. What am I missing?

9
  • What do you mean by importing that page using AJAX? Commented Oct 30, 2015 at 0:22
  • Maybe that was the wrong term. But basically, using AJAX to filter results from the db. The external file AJAX is using within the page contains these commands. Commented Oct 30, 2015 at 0:25
  • Do you mean that the above snippet is added to DOM via AJAX call? Commented Oct 30, 2015 at 0:27
  • Yes sir, that's exactly what I mean. Please excuse my lack of clarity. This is all so new to me. Commented Oct 30, 2015 at 0:33
  • It works here: jsfiddle.net/mqr0nsvj Commented Oct 30, 2015 at 0:36

5 Answers 5

1

Try registering an event handler using jQuery's .ajaxcomplete http://api.jquery.com/ajaxcomplete/ . This event gets fired when an AJAX request finishes which is probably what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

0

User the following way to update the content using Ajax

<div id="demo">Let AJAX change this text</div>

<button type="button" onclick="loadDoc()">Try it</button>

<script>
function loadDoc() {
    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("demo").innerHTML = xhttp.responseText;
        }
    }
    xhttp.open("GET", "ajax_info.txt", true);
    xhttp.send();
}
</script>

Go to following link for more ajax examples. Ajax Examples

2 Comments

But this still requires a button, which is what I'm trying to eliminate.
What exactly is your requirement?
0
<script>
$(document).ready(function(){
$.ajax({
  url: "ajax_info.txt"
}).done(function( data ) {
    $("#demo").html(data);
  });

});
</script>

on ready function you can call ajax and change the content

Comments

0

Use jquery ready function and call your function inside ready function. like this :

$(document).ready(function(){
  myFunction();
});

2 Comments

Such as this? $("document").ready(function() { $("#demo").text("Yes, changed"); $("#demo").html("Yes, changed"); }); -- Bcuz again it works on the page alone, but used with AJAX and it does not. What is sooooo strange, is that I have it working just fine on another site, but I can't seem to isolate what the difference is.
You can use on click instead of ready function with ajax. something like this : $(document).on('click',function(){myFunction(); }); OR call function after ajax success
0

/* jQuery Onload String Replace OR jQuery Onload String Change */

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript"> 
$(document).ready(function() {
    var strNewString = $('body').html().replace(/\On hold/g,'Processing');
    $('body').html(strNewString);
});

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.