skip to Main Content

I have a external file with text inside then that is update everytime.

<div id='1'>TEST 1</div>
<div id='2'>TEST 2</div>
<div id='3'>TEST 3</div>

This archive is called IO.HTML, My Script is getting all the information inside this archive and putting then in only one div, with all the information, I want to separated and update the specific ID every second, with only the text inside it, for example:

$(document).ready(function(){
    $.ajaxSetup({ cache: false });
setInterval(function() {
    $.get("IO.html", function(result){
        $('#1').text(result.trim());
        $('#2').text(result.trim());
        $('#3').text(result.trim());
    });
},1000);
});

html:

<div class="Takt" id="1"></div>
<div class="Takt" id="2"></div>
<div class="Takt" id="3"></div>

But how I say, in the ID 1, all the information in the archive IO.html is writing inside then, How can I separated this information with only the text inside the ID?

2

Answers


  1. To load the appropriate content use:

    $('#1').load('IO.html #1');
    

    Therefore to load all the three divs use:

    [1,2,3].map(a => $('#' + a).load('IO.html #' + a));
    

    REFERENCE

    Login or Signup to reply.
  2. Wrap the result in $() so you can use jQuery methods to extract the relevant parts of it the same wy you would

    $.get("IO.html", function(result){
         const $doc = $(result);
         $.each([1,2,3], function(_, n){
            // use `filter()` instead of `find()` if the elements are not 
            // in a wrapping container
            $('#' + n).html( $doc.find('#' + n).html())
         });     
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search