-1

I am using java script after making an ajax call to display the data in a div with new JS content. Please refer to the code below:

//ajax call from a.jsp

var goUrl = "/testMethod/getTestMethod;
            var httpRequest=null;
            var refreshContent = "null";
            httpRequest = XMLHTTPObject();
            httpRequest.open("POST", goUrl, true);
            httpRequest.onreadystatechange = function () {ajaxFunction(refreshThisDiv,httpRequest); } ;
            httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            httpRequest.send(null);



function ajaxFunction(refreshThisDiv,httpRequest){

    var serversideValidation = true;
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
        {
            results =  httpRequest.responseText;  // http.responseXML; which will lead to an XML based response, if we were to have some XML output from a server file

            if(results != 'null') {
                var test= document.getElementById(refreshThisdiv);

                test.style.display = '' ;
                test.innerHTML =  results;


            }

//Below is in b.jsp which is new content to display.

<div id="test">
</div>
<script>
 var test = document.getElementById("test");
test.innerHTML ="HI";
</script>

Results are coing fine and redirecting to the b.jsp and displaying the html content. But tags are not working :(

I want to see Hi after ajax call is completed for that div. Please help me.

user3213490
  • 157
  • 1
  • 9
  • In what way are they "not working"? Are you getting a specific error message? – Robert Columbia Aug 23 '16 at 04:51
  • – user3213490 Aug 23 '16 at 05:00
  • 1
    Whatever is there in the script, please add it after `test.innerHTML = results;` – vijayst Aug 23 '16 at 05:02
  • @Vijay thanks a lot. I got the mistake and your suggestion worked out. – user3213490 Aug 23 '16 at 06:37

2 Answers2

4

The ID is not #test, but test. #test is a selector that you'd use with jQuery, CSS or document.querySelector. document.getElementById requires, unsurprisingly, the ID. :)

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Hi @Amadan thanks for the correction. But still after making the ajax call is completed I need the javascript. Even alert is not coming up in the new jsp content – user3213490 Aug 23 '16 at 03:37
  • 1
    Sorry, but... there is no Ajax in your posted code. We can't fix what we can't see. – Amadan Aug 23 '16 at 03:38
  • Hi @Amadan. I have added the code. Please check it out. – user3213490 Aug 23 '16 at 04:11
  • So basically you're adding stuff with `innerHTML` and expecting it to execute scripts in it? This might be a duplicate: [Can scripts be inserted with innerHTML?](http://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml) – Amadan Aug 23 '16 at 05:37
2

getElementById() just needs the name of the ID. You incorrectly used CSS-ish syntax by passing #test where only test is needed.

Corrected new code:

<div id="test">
</div>
<script>
  var test = document.getElementById("test");
  test.innerHTML ="HI";
</script>
matthew.
  • 176
  • 5
  • hi @Matt thanks for the correction. But still after making the ajax call is completed I need the javascript. Even alert is not coming up in the new jsp content – user3213490 Aug 23 '16 at 03:38
  • Could possibly update your question and provide a more complete code example? There is no ajax in your original example. – matthew. Aug 23 '16 at 03:49
  • Hi @Matt. I have added the code . Please check it out. – user3213490 Aug 23 '16 at 04:10
  • Can anyone help me in splitting a javascript list into two different lists? I have a javascript list. Using a delimeter I want to split that into two lists.Here I am using ':' as a delimeter. saye we have a list a as: list a = [1,2,3,:,4,5] Now I want to split that into two different lists as list b = [1,2,3] list c =[4,5] Can anyone Please suggest the optimal solution using javascript / jquery. – user3213490 Aug 23 '16 at 07:52
  • a) `[1,2,3,:,4,5]` is not a valid JavaScript list, so we can't know how to answer this. b) While it is okay to ask for clarifications on the current question, this is completely unrelated: post as a new question. – Amadan Aug 24 '16 at 06:48