1
votes

I have a Drupal view which is a list of nodes. There is a node-type.tpl.php containing Jquery javascript in the head section. Clicking a link in any node should load a new ajaxed view referenced to the clicked node. I am using ajax to load the new view via a hook_menu path with the original node as the node reference argument. Unfortunately this click currently only loads a view referenced to the very first node in the list rather than the clicked node. No matter what node I click I only get a view referenced to the very first node in the list. I cannot understand why the javascript isn't sending the correct node id for the argument. Here is the code in the head section of node-type.tpl.php

 <script>
    Drupal.behaviors.myajax = function(context) {
    $(".ajaxclick").click(function(){
    <?php $mynid = $node->nid;?>
    var mynodeid = <?php echo $mynid ?>;
    $(".holder").load(Drupal.settings.basePath + "myajax/" + mynodeid);
   });
  }
 </script>

And in the body;

<a class="ajaxclick" href= "#">Click me</a>
<div class="holder"></div>

And here is the hook_menu code in the module:

 <?php
 function ajaxview_menu() {
   $items = array();
   $items['myajax'] = array(
     'title' => 'My Ajax',
      'page callback' => 'ajaxview_myajax',
     'description' => 'Ajax View',
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
   );
   return $items;
 }
 function ajaxview_myajax($nid) {
   $node = node_load($nid);
   print '<div class="rightcolumn">';
   print $viewName = 'Comments';
   $display_id = 'default';
   $myArgs = $nid;
   print views_embed_view($viewName, $display_id, $myArgs);
   print '</div>';
   exit();
  }

Anybody know why how to make the jquery javascript recognise separate node ids for each node?

Or is there a better way?

1

1 Answers

0
votes

I had the very same problem and finally got help by @William Tate

Solution:

$('.up2').click(function() {
    $(this).closest('.up2').find('.nummer_dark').text();
}

My question: Drupal 7 + jQuery, how to get jQuery to interact only with the active node (via nid) and not every node in the list