-4

I have a html file with many div. Beside this I have a JQuery file too which run an AJAX request in every 30 seconds. In my JQuery If a condition met and the jQuery reloded 3 times I want to update my div-s. I tried to add a div, but it does not appear just when I reload my whole page. If I set to remove my div and not add, the JQuery removes my div, so it is very odd for me, because just the .add or .append or .html do not working. furthermore I set a class with invisibility too, and I also set that when the condition met the jQuery file remove the invisibility class, but the div do not want to appear. I am trying to create a sample code.

My html

<div  class="row">
      <div  id="myclass" class= "invisible" >
           <div <p> Please appear for me!<p></div>
      </div>  
</div>

My JQuery:

if (condition) {
    $('#myclass').removeClass('invisible');

    if (n >= 2) {
        $('#myclass').addClass('invisible');
    }
}   

The main point is, If the conditions met the class not removes just When I reload my page. In the jQuery file the n is equal with the times when the AJAX reloaded with setInterval. My question is why my div not appear just when I reload my whole page? Also why remove without reload?

Thank you in advance the answers!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bummm26
  • 159
  • 3
  • 17
  • I also tried add the whole div like these: `$('#myclass').append('

    Please appear for me!

    '); , $('#myclass').html('

    Please appear for me!

    '); , $('#myclass').insertAfter('

    Please appear for me!

    '); `

    – bummm26 Jun 20 '17 at 08:39
  • Possible duplicate of [Refresh/reload the content in Div using jquery/ajax](https://stackoverflow.com/questions/18490026/refresh-reload-the-content-in-div-using-jquery-ajax) – mrhallak Jun 20 '17 at 09:01

1 Answers1

0

removeClass and addClass perfectly work. at first, you can check if you jQuery works, just try to write you script (with ".append",".html",".insertAfter") directly in browser console, else you can add some console.log in your code, when you change invisibility like:

console.log('removeClass');
$('#myclass').removeClass('invisible');
if (n >= 2) {
    console.log('addClass');
    $('#myclass').addClass('invisible');
}

for update some div content, you can use simple function for it. like - call self page in get request, and replace div content.

function reloadMyDiv(selector){
    var element = $(selector);
    if(element.length===1){
        $.get(window.location.href,{},function(response){
            var new_page = $('<div></div>').append(response);
            var new_element = new_page.find(selector);
            if(new_element.length===1){
                element.html( new_element.html() );
                console.log('update');
            }else{
                console.log('need contains only one element, '+new_element.length+' found!');
            }
        });
    }else{
        console.log('need contains only one element, '+element.length+' found!')
    }
};

and if you more _ not use your interval, is good practice for clear it.

var n = 0;
var myInterval = setInterval(function(){ myFunction() },30*1000);
function myFunction(){
    n+=1;
    //do, what you want to do
    if(n>2){
        clearInterval(myInterval);
    }
}
Ygrick
  • 86
  • 1
  • 4