0

I have created anonymous function to load my data via ajax. Here is my code :-

jQuery(function(){
    jQuery("[data-load]").each(function(){
        jQuery("#"+jQuery(this).data('area')).load(jQuery(this).data("load"), function(){alert( "Load was performed." );});
    });
});

Here is my HTML Code :-

<div data-load="header.php" data-area="_page_1">
<span id="logo">Travel</span>
<span id="_page_1">Link_1 Link_2</span>
</div>

I am using ajax load function :- http://api.jquery.com/load/

Thanks

John Cargo
  • 1,839
  • 2
  • 29
  • 59

1 Answers1

1

You have to specify the element for what attribute you are looking for. Line 2 should be

jQuery("div[data-load]").each(function(){

Here is the doc http://api.jquery.com/has-attribute-selector/

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
cujo
  • 368
  • 3
  • 16
  • Have you confirmed that header.php works? Can you copy the error message that is in the javascript console to here. – cujo Jun 12 '14 at 17:21
  • There is no error, but ajax load function does not load. header.php file is there and is specified with correct path too. I think `load` event is not getting triggered i guess – John Cargo Jun 12 '14 at 17:29
  • use `console.log('Some random text');` to make your code verbose. Also watch the network tab on chrome developer console to see if the request is made – cujo Jun 12 '14 at 17:40
  • i am watching `network tab`, and instead of `console.log` i used `alert('test');` before `load function`, it game me `alert box` , but when i added same alert box in `callback` of `load`. There was no pop up of alert. – John Cargo Jun 12 '14 at 18:45
  • I looked a little bit harder at your selector, it looks like there might be a missing #. In order to identify ids your selector for _page_1 should be `#_page_1` but based on your code you may need to append that to the front of the selector like this `jQuery("#" + jQuery(this).data('area')).load(jQuery(this).data("load"), function(){alert( "Load was performed." );});` – cujo Jun 12 '14 at 18:54
  • 1
    Also according to this post, http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html HTML IDs must begin with a Letter which that does not. – cujo Jun 12 '14 at 18:56