0

I have the below jquery function to load data from database which works fine, however I would like to show that same data again over the same page, When I do that it doesn't work. it will only shows for the first one. could someone help me out?

     <script type="text/javascript">
  $(document).ready(function() {
    loadData();
});

var loadData = function() {
    $.ajax({    //create an ajax request to load_page.php
        type: "GET",
        url: "second.php",             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
            $("#responsecontainer").html(response);
            setTimeout(loadData, 1000); 
        }

    });
};
 </script>

 <div style='display:inline' id='responsecontainer'></div><!--only this one is displayed-->
 <div style='display:inline' id='responsecontainer'></div><!-- I want it to show me the same data here too-->
2
  • also cache the html so it can be reused instead of multiple requests Commented Nov 16, 2013 at 0:30
  • 1
    Your are assigning a identical id to 2 different elements, an id must be unique, change the id of the second div or just use classes instead ids. Commented Nov 16, 2013 at 0:33

1 Answer 1

2

ID of an element must be unique in a document, use class attribute to group similar elements

<div style='display:inline' class='responsecontainer'></div>
<div style='display:inline' class='responsecontainer'></div>

then use class selector

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

var loadData = function () {
    $.ajax({ //create an ajax request to load_page.php
        type: "GET",
        url: "second.php",
        dataType: "html", //expect html to be returned                
        success: function (response) {
            $(".responsecontainer").html(response);
            setTimeout(loadData, 1000);
        }

    });
};

The ID selector will return only the first element with the given ID

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.