jquery - Button selects id -


so have these buttons , want them show section same id.

<aside>     <button type="button" id="n">16-50mm</button>     <button type="button" id="j">25-75mm</button>     <button type="button" id="m">30-50mm</button>     <button type="button" id="g">50-120mm</button>     <button type="button" id="k">50-100mm</button>     <button type="button" id="a">75-200mm</button>     <button type="button" id="c">75-200mm</button>     <button type="button" id="f">75-150mm</button>                   </aside>  <section id="n">     <figure>         <a href="a1.html"><img src="images/000-001.jpg" alt="image1 a1"></a>         <br>         <figcaption>name</figcaption>     </figure> </section>  <section id="j">     <figure>         <a href="a1.html"><img src="images/000-001.jpg" alt="image2 a1"></a>         <br>         <figcaption>name2</figcaption>     </figure> </section> 

i saw somewhere people use data-id or data-attribute show specific id. can make <button type="button" data-id="n">16-50mm</button> , should show <section id="n">. appreciate if me out this.

as you've seen cannot have duplicate id attributes in single document. better approach use data attributes. try this:

$('aside button').click(function() {   $('section').hide().filter('#' + $(this).data('id')).show(); }); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <aside>   <button type="button" data-id="n">16-50mm</button>   <button type="button" data-id="j">25-75mm</button>   <button type="button" data-id="m">30-50mm</button>   <button type="button" data-id="g">50-120mm</button>   <button type="button" data-id="k">50-100mm</button>   <button type="button" data-id="a">75-200mm</button>   <button type="button" data-id="c">75-200mm</button>   <button type="button" data-id="f">75-150mm</button> </aside>  <section id="n">   <figure>     <a href="a1.html">       <img src="images/000-001.jpg" alt="image1 a1">     </a>     <br>     <figcaption>name</figcaption>   </figure> </section>  <section id="j">   <figure>     <a href="a1.html">       <img src="images/000-001.jpg" alt="image2 a1">     </a>     <br>     <figcaption>name2</figcaption>   </figure> </section> 

working example


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -