Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.