Jump to content

Displays DIV whose ID equals the ID of the clicked SPAN


soltek

Recommended Posts

Here I am once again, guys =)

 

First, my markup:

 

<div class="items_list">
        <span ID="1">Item</span>
        <span ID="2">Item</span>
        <span ID="3">Item</span>
</div>
<div class="content_list">
        <div id="1">Content</div>
        <div id="2">Content</div>
        <div id="3">Content</div>
</div>

So, all the divs inside .contant_list are hidden using css, what I'm trying to do is to make them visible, once the correspondent span is clicked.
For example, you click on the second span, with ID=1, and the div with the same ID would become visible.

Can you help me do that?
If this method isn't... suitable, you can suggest me another ones.


Thanks!


Hi,

Normally, the ID should be unique, so, no 2 elements with same ID. You must think to other solution.

Here's an example:

<div class="items_list">
  <span ID="s1" title="d1" onclick="showDiv(this.title)">Item</span>
  <span ID="s2" title="d2" onclick="showDiv(this.title)">Item</span>
  <span ID="s3" title="d3" onclick="showDiv(this.title)">Item</span>
</div>
<div id="content_list">
  <div id="d1">Content1</div>
  <div id="d2">Content2</div>
  <div id="d3">Content3</div>
</div>

<script type="text/javascript"><!--
// JS tabs, by www.coursesweb.net
function showDiv(id) {
  var divs = document.getElementById('content_list').getElementsByTagName('div');
  for(var i=0; i<divs.length; i++) divs[i].style.display = 'none';
  document.getElementById(id).style.display = 'block';
}
--></script>

Here's with jQuery:

<div class="items_list">
  <span ID="s1" title="d1">Item</span>
  <span ID="s2" title="d2">Item</span>
  <span ID="s3" title="d3">Item</span>
</div>
<div id="content_list">
  <div id="d1">Content1</div>
  <div id="d2">Content2</div>
  <div id="d3">Content3</div>
</div>

<script type="text/javascript"><!--
// JS tabs, by www.coursesweb.net
$(document).ready(function() {
  $('.items_list span').click(function(){
    $('#content_list div').hide();
    $('#'+this.title).show('slow');
  });
});
--></script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.