3

I have a Views Accordion displaying some books under their categories. I want to add some jQuery interaction, so when the user clicks the triangle of each category, the content will be loaded via jQuery AJAX. How can I include this functionality? Something like this, though I cannot see any ID identifier associated with the href which opens / closes each of the accordion bellows.

 function mymodule_views_pre_render(&$view) {
     if($view->name == "categorias"){
         drupal_add_js('jQuery(function() {
             jQuery("#categorias-page-0").click(function(e) {
              // ... AJAX stuff
              });                    
         });', 'inline');
     } // if        
 } // function

Show image below, what I have, and when clicking on each category I would like to perform whatever jQuery actions I'd like.

enter image description here

1
  • This is a plain jQuery question. Drupal doesn't change how to use $(this) in JavaScript.
    – apaderno
    Aug 25, 2016 at 11:48

2 Answers 2

2

You have to use $(this) with out working on id/class.

Refer to this link jQuery $(this) Selector with Examples.

2

Using suggestions by #Nagarjuna , I have updated the code so I can retrieve the internal ID of the accordion bellow. The only problem is that the numbers 0, 1, etc., are just a numbering, but using the same SORTING than the Views Query, they should match perfectly when accesing the content via AJAX.

 function mymodule_views_pre_render(&$view) {
   if($view->name == "categorias"){
     drupal_add_js('jQuery(document).ready(function () { jQuery(function () { 
       jQuery(\'a[href ^= "\' + "#categorias-page-" + \'"]\').each(function() {
            jQuery(this).click(function(){ 
              var clickado = (this + "");
              var categoria = clickado.split("-");
              alert(categoria[categoria.length - 1]);
            });
        });
    }); }(jQuery));', 'inline');
 } // if        
} // function

Not the answer you're looking for? Browse other questions tagged or ask your own question.