4

I am looking for information what is the simplest way to update page content using jquery + ajax. I would like to update only #container content, but not a full page.

I am begginer in ajax.

Thanks.

MY Jquery code

  <script>
    $(document).ready(function(){
      //page
      $('.row').load('services.html');

      $('.nav_link').click(function(){
        var page = $(this).attr('href');
        $('.row').load(page);
        return false;
      });
    });
  </script>

HTML:

<div class="row">
          <nav role="navigation" class="nav">
            <a class="nav_link" href="services"><div class="nav_line"></div>PASLAUGOS</a> <a class="nav_link" href="contacts.html"><div class="nav_line"></div>KONTAKTAI</a>
          </nav>
        </div>

services.html:

<div class="row">
    <nav role="navigation" class="nav">
        <a class="nav_link" href="services.html">
            <div class="nav_line"></div>
            PASLAUGOS
        </a> 
        <a class="nav_link" href="contacts.html">
            <div class="nav_line"></div>
            KONTAKTAI
        </a>
    </nav>
    <div class="result">
      gsdgdsgsdgdsgds gds gdsg dsg sd gds g sdgsd gdsg sd gsd
    </div>
</div>
Julius J
  • 103
  • 1
  • 2
  • 8

1 Answers1

11

Try

$('.row').on("click",".nav_link",function(e){ 
  e.preventDefault(); // cancel click
  var page = $(this).attr('href');   
  $('.row').load(page);
});

Since .on will assign the click to the new links

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Is there any alternative for $('.row').load(page);? I need to open new page in the whole of window, i mean i dont want to open it into an element, i want to replace all current page with another one, is that possible? – Martin AJ Sep 05 '16 at 10:50
  • 4
    `location.href=page` – mplungjan Sep 05 '16 at 11:22
  • Great, thank you. Just look, my reason of doing that is making a "back button", So I need to select the whole content of current page *(even `` tag)* and keep it into a JS array to use it when user clicks on "back button". Anyway, can you please tell me how can I select the whole things of current page? – Martin AJ Sep 05 '16 at 11:35
  • Please ask a question instead of hijacking this one :) It is not really related – mplungjan Sep 05 '16 at 11:44
  • [well my friend did](http://stackoverflow.com/questions/39330813/how-can-i-create-a-back-forward-button).. If you have some free time, please take a look at it :) – Martin AJ Sep 05 '16 at 12:39