jquery - click on different images returns same id

128 views Asked by At

I've been struggling with this for a while.

On my page I have 17 images and 17 hidden blocks. Each image is supposed to show a specific block, that is right underneath in the css.

Here is the HTML of the images to be clicked and the jquery I've been trying.

No matter which image I click it always saves the same ID.

The site is on Drupal and there's a lot of and I guess I am not managing to reach the section ID properly. Ultimately what I want to do is save the ID in a variable and then go through the list of id I've got and show the appropriate block. Do I make sense?

Help would be much appreciated as I am not confident in jquery

jQuery('.impact-sdg-title').click(function() {
    var idPix2 = jQuery('.sdg-pix-first').attr('id');
    var idProject2 = jQuery(this).find('.sdg-class-block').attr('id');    
    jQuery('#'+idProject2).toggle();
    console.log('idProject2:'+idProject2+' - idPix2:'+idPix2);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<section id="block-views-sdg-impact-page-block-2" class="block block-views sdg-pix-first clearfix">

<div class="view view-sdg-impact-page view-id-sdg_impact_page view-display-id-block_2 view-dom-id-735e3592c6267da0604395442ff6c98c">

<div class="lead">
  </div>

<div class="view-impact">
  
        <div class="view-filters">
                </div>
  
    <div class="view-content-impact">
        <div>
    
<div id="img-click-sdg-332296" class="sdg-block-solo text-center" >
 <a class="impact-sdg-title">
  <div class="inside-impact-title">
   No poverty  </div>
 </a>
</div>  </div>
    </div>
  
        <div class="views-pager">
            </div>
    
<div class="lead">
  </div>

</div> 

</div>

</section>
<section id="block-views-e38d75a566d98546e16f91cf4e411ed1" class="block block-views col-xs-12 col-sm-12 col-md-10 col-lg-10 center-block sdg-class-block clearfix" style="display: none;">

        <h2 class="block-title">SDG 2 projects</h2>
    
  <div class="view view-sdg-s-lists-for-import-page-2017 view-id-sdg_s_lists_for_import_page_2017 view-display-id-sdg_2 view-dom-id-1383c4e5819ae713cf4b02b420f9c90e">
        
  
  
      <div class="view-content">
        <div class="views-row views-row-1 views-row-odd views-row-first views-row-last">
    
 <div class="sdg-row-per-sdgs col-xs-12 col-sm-12 col-md-12 col-lg-12">
  <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
     </div>
  <div class="col-xs-12 col-sm-12 col-md-7 col-lg-7">
   <h2 class="field-content">SDG 2</h2>   <p></p>
   <div class="sdg-ambass"> - </div>
   <div class="sdg-links">    </div>
   <div class="sdg-img-gallery sdg-effectback">
       
   </div>
  </div>
 </div>
  </div>
    </div>
  
  
</div>
</section>

<section id="block-views-sdg-impact-page-block-3" class="block block-views sdg-pix-first clearfix">

  

<div class="view view-sdg-impact-page view-id-sdg_impact_page view-display-id-block_3 view-dom-id-8f785cce6961f8aa91168d35c844e4fc">

<div class="lead">
  </div>



<div class="view-impact">
  
        <div class="view-filters">
                </div>
  
    <div class="view-content-impact">
        <div>
    
<div id="img-click-sdg-332306" class="sdg-block-solo text-center" style=">
 <a class="impact-sdg-title">
  <div class="inside-impact-title">
   Zero Hunger  </div>
 </a>
 
</div>  </div>
    </div>
  
      
        <div class="views-pager">
            </div>
    
<div class="lead">
  </div>

</div> <!-- /views-view -->

</div>

</section>
<section id="block-views-e64c24a7956cb576184ba38013f7788a" class="block block-views col-xs-12 col-sm-12 col-md-10 col-lg-10 center-block sdg-class-block clearfix" style="display: none;">

        <h2 class="block-title">SDG 3 projects</h2>
    
  <div class="view view-sdg-s-lists-for-import-page-2017 view-id-sdg_s_lists_for_import_page_2017 view-display-id-sdg_3 view-dom-id-939e64096495ca38b10a0e262e43bed3">
        
  
  
      <div class="view-content">
        <div class="views-row views-row-1 views-row-odd views-row-first views-row-last">
    
 <div class="sdg-row-per-sdgs col-xs-12 col-sm-12 col-md-12 col-lg-12">
  <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
     </div>
  <div class="col-xs-12 col-sm-12 col-md-7 col-lg-7">
   <h2 class="field-content">SDG project 3</h2>   <p></p>
   <div class="sdg-ambass"> - </div>
   <div class="sdg-links">    </div>
   <div class="sdg-img-gallery sdg-effectback">
       
   </div>
  </div>
 </div>
  </div>
    </div>
  
  
</div>
</section>

1

There are 1 answers

1
guradio On

jQuery('.impact-sdg-title').click(function() {
    var idPix2 = jQuery(this).closest('.sdg-pix-first').attr('id');
    var idProject2 = jQuery(this).closest('section').next('section.sdg-class-block').attr('id');    
    jQuery('#'+idProject2).toggle();
    console.log('idProject2:'+idProject2+' - idPix2:'+idPix2);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<section id="block-views-sdg-impact-page-block-2" class="block block-views sdg-pix-first clearfix">

<div class="view view-sdg-impact-page view-id-sdg_impact_page view-display-id-block_2 view-dom-id-735e3592c6267da0604395442ff6c98c">

<div class="lead">
  </div>

<div class="view-impact">
  
        <div class="view-filters">
                </div>
  
    <div class="view-content-impact">
        <div>
    
<div id="img-click-sdg-332296" class="sdg-block-solo text-center" >
 <a class="impact-sdg-title">
  <div class="inside-impact-title">
   No poverty  </div>
 </a>
</div>  </div>
    </div>
  
        <div class="views-pager">
            </div>
    
<div class="lead">
  </div>

</div> 

</div>

</section>
<section id="block-views-e38d75a566d98546e16f91cf4e411ed1" class="block block-views col-xs-12 col-sm-12 col-md-10 col-lg-10 center-block sdg-class-block clearfix" style="display: none;">

        <h2 class="block-title">SDG 2 projects</h2>
    
  <div class="view view-sdg-s-lists-for-import-page-2017 view-id-sdg_s_lists_for_import_page_2017 view-display-id-sdg_2 view-dom-id-1383c4e5819ae713cf4b02b420f9c90e">
        
  
  
      <div class="view-content">
        <div class="views-row views-row-1 views-row-odd views-row-first views-row-last">
    
 <div class="sdg-row-per-sdgs col-xs-12 col-sm-12 col-md-12 col-lg-12">
  <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
     </div>
  <div class="col-xs-12 col-sm-12 col-md-7 col-lg-7">
   <h2 class="field-content">SDG 2</h2>   <p></p>
   <div class="sdg-ambass"> - </div>
   <div class="sdg-links">    </div>
   <div class="sdg-img-gallery sdg-effectback">
       
   </div>
  </div>
 </div>
  </div>
    </div>
  
  
</div>
</section>

<section id="block-views-sdg-impact-page-block-3" class="block block-views sdg-pix-first clearfix">

  

<div class="view view-sdg-impact-page view-id-sdg_impact_page view-display-id-block_3 view-dom-id-8f785cce6961f8aa91168d35c844e4fc">

<div class="lead">
  </div>



<div class="view-impact">
  
        <div class="view-filters">
                </div>
  
    <div class="view-content-impact">
        <div>
    
<div id="img-click-sdg-332306" class="sdg-block-solo text-center" style=">
 <a class="impact-sdg-title">
  <div class="inside-impact-title">
   Zero Hunger  </div>
 </a>
 
</div>  </div>
    </div>
  
      
        <div class="views-pager">
            </div>
    
<div class="lead">
  </div>

</div> <!-- /views-view -->

</div>

</section>
<section id="block-views-e64c24a7956cb576184ba38013f7788a" class="block block-views col-xs-12 col-sm-12 col-md-10 col-lg-10 center-block sdg-class-block clearfix" style="display: none;">

        <h2 class="block-title">SDG 3 projects</h2>
    
  <div class="view view-sdg-s-lists-for-import-page-2017 view-id-sdg_s_lists_for_import_page_2017 view-display-id-sdg_3 view-dom-id-939e64096495ca38b10a0e262e43bed3">
        
  
  
      <div class="view-content">
        <div class="views-row views-row-1 views-row-odd views-row-first views-row-last">
    
 <div class="sdg-row-per-sdgs col-xs-12 col-sm-12 col-md-12 col-lg-12">
  <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
     </div>
  <div class="col-xs-12 col-sm-12 col-md-7 col-lg-7">
   <h2 class="field-content">SDG project 3</h2>   <p></p>
   <div class="sdg-ambass"> - </div>
   <div class="sdg-links">    </div>
   <div class="sdg-img-gallery sdg-effectback">
       
   </div>
  </div>
 </div>
  </div>
    </div>
  
  
</div>
</section>

  1. You want closest with next to find the section with the class you are looking for