javascript - Ajax test (wordpress) -


so, have been struggling making ajax work. here previous question: ajax (admin_url('admin-ajax.php');?action=) not found

anyway, decided narrow down , have necessary files.

here set up.

test.php

  <div class="test">        <a href="#test_demo" id="demo">items</a>         </div>   <div id="test_demo"> </div>     <script>    jquery(document).ready(function() {      jquery('.test a').click(function(e) {         e.preventdefault();          var tab_id = jquery('this').attr('id');            jquery.ajax({             type: "get",             url: "<?php echo admin_url('admin-ajax.php'); ?>",              datatype: 'html',             data: ({ action: 'test_tab', id: tab_id}),             success: function(data){                   jquery('#test_' + tab_id).html(data);          },         error: function(data)           {           alert("error!");         return false;         }            });        });       });    </script> 

function.php

function test_tab_callback() {            $template_part_path = 'page-parts/test_' . $_get['id'];     get_template_part($template_part_path);     exit;  } add_action('wp_ajax_test_tab', 'test_tab_callback'); add_action('wp_ajax_nopriv_test_tab', 'test_tab_callback'); 

test_demo.php

<div id="test_demo_content">demo content</div> 

here idea on how should work.

  1. test.php: when user clicks items button, tab_idvariable in jquery saves anchor id (in case, id="demo").
  2. then admin-ajax.php called.
  3. the saved id ("demo") passed onto function.php , used in variable $template_part_path = 'page-parts/test_' . $_get['id']; gives page-parts/test_demo test_demo.php
  4. then template part called , calledback jquery.

  5. then data "insert" jquery('#test_' + tab_id).html(data); id="test_demo.

  6. the test_demo.php content should displayed in #test_demo div.

but not working. used console.log(data) showed no result. doing wrong?

when getting jquery object of "this" can't use quotes. making tab_id undefined , ruining rest.

here mean:

http://jsfiddle.net/7r1dg7l4/2/

jquery(document).ready(function() {      jquery('.test a').click(function(e) {          e.preventdefault();          var tab_id = jquery(this).attr('id');           alert(tab_id);      }); }); 

Comments