0

I would like to change the content inside <div id="homePagePosts"> to be the new data from the server. The new data is in HTML format rendered by the server. However, the old HTML still stays the same after the AJAX data has been received by the browser.

When I console.log as shown below, I can see the rendered new HTML data is shown in the console. I have placed both Javascript and JQuery file at the bottom of the <body> but still doesn't work. I'm not too sure where it goes wrong here.

HTML

<div id="homePagePosts">
    <% include ../ajaxSnippets/mainHome %>
</div>

Javascript

$.ajax({
      url: "/main/home",
      type: "POST",
      contentType: "application/json",
      data: JSON.stringify(sendData)
  }).done(function(result){
      //This shows up in the console
      console.log(result)
      updateMainPagePosts(result);
  }).fail(function(err){
      console.log(err);
  });

var updateMainPagePosts = function(posts){
  document.getElementById("homePagePosts").innerHTML = posts;
};
6
  • What do you see in the browser console? What do you see when you use the debugger built into your browser to step through the code statement by statement? (E.g., set a breakpoint on the console.log(results), look at results, step into updateMainPagePosts, look at what happens, etc.) Are you sure you don't have two elements with that id? (Only the first would be updated.) Fundamentally, if you have an element with id="homePagePosts" in your document, the code document.getElementById("homePagePosts").innerHTML = posts; (with posts being a parameter as shown) will update it. Commented Mar 15, 2018 at 8:29
  • Just wrap the whole JS above inside $(document).ready(function(){ }); It should work after that. Commented Mar 15, 2018 at 8:37
  • @LukeP.Issac: No, there's no need for that at all. Commented Mar 15, 2018 at 8:38
  • The code as shown works. Until or unless the OP can provide more details, there's no point trying to answer this question. Steven, you've said you see result logged with the expected content (so the ajax call works). I'm afraid we can't help you further as the code from that point (calling updateMainPagePosts and the code within it) is correct. Debugging is the only way you'll find out what's happening. (It's also the first thing to do anyway. :-) ) Good luck! Commented Mar 15, 2018 at 8:38
  • Stack Overflow is a very active place. When you post a question (or answer), stick around for a few minutes to answer requests for more information, etc. Commented Mar 15, 2018 at 8:43

1 Answer 1

1

You have defined the function as a variable and is not usable in this method.

change this :

var updateMainPagePosts = function(posts){
  document.getElementById("homePagePosts").innerHTML = posts;
};

to this :

function updateMainPagePosts(posts){
  document.getElementById("homePagePosts").innerHTML = posts;
}

More details from: https://www.quora.com/Is-the-var-x-function-the-same-as-the-function-x-in-JavaScript:

This will break. It uses a Function Expression which is being assigned to a variable. console.log(myFunction(1, 2)); var myFunction = function (a, b) { return a + b; };

This will print '3' to your console. It uses a Function Declaration. console.log(myFunction(1, 2)); function myFunction(a, b) { return a + b; }

The difference is that in the example with a function Definition, the function is being "hoisted" to the start of the scope (which could be a parent function, or the file, for example) by the JavaScript interpreter, which means anywhere within that scope it can be called and it will execute the function you have written.

In the function Expression the function is being passed around as if it were a value and is interpreted whenever it is asked to be. In the example we're seeing here, it, as a value is being assigned to the myFunction variable. Since we put a 'var' statement in front of that variable, then myFunction that variable is being hoisted, but its value, the function expression, is not hoisted (do not get the two confused).

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

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.