views:

342

answers:

3

Can Somebody please tell me why this script is not working.. Its supposed to work, but doesnt, I am getting the id correctly, but Div's are not displaying properly.. My idea is to display one div based on the click, and hide the other Div's. Please help..

Script

$(document).ready(function() {
    $("a").live("click", function(){
    var idV = $(this).attr("id"); 
    alert(idV);
    $("#"+idV+"div").css("display","block");
    return false;
    });
});

HTML

<a href="#" id="solution1">Solution 1</a>
<a href="#" id="solution2">Solution 2</a>
<a href="#" id="solution3">Solution 3</a>
<a href="#" id="solution4">Solution 4</a>
<br />

<div id="solution1" style="display:none;">Solution 1</div>
<div id="solution2" style="display:none;">Solution 2</div>
<div id="solution3" style="display:none;">Solution 3</div>
<div id="solution4" style="display:none;">Solution 4</div>
+5  A: 

Your div ids are wrong. Try:

<div id="solution1div" style="display:none;">Solution 1</div>

instead of

<div id="solution1" style="display:none;">Solution 1</div>

Edit:

JSBIN: Preview

JSBIN: Source Code

<a href="javascript:;" id="solution1">Solution 1</a> 
<a href="javascript:;" id="solution2">Solution 2</a> 
<a href="javascript:;" id="solution3">Solution 3</a> 
<a href="javascript:;" id="solution4">Solution 4</a> 
<br /> 

<div> 
<div id="solution1div" style="display:none;">Solution 1</div> 
<div id="solution2div" style="display:none;">Solution 2</div> 
<div id="solution3div" style="display:none;">Solution 3</div> 
<div id="solution4div" style="display:none;">Solution 4</div> 
</div>

jquery:

$("a").live("click", function(){ 
var idV = $(this).attr("id");  

$("#"+idV+"div").siblings().hide(); 
$("#"+idV+"div").show(); 

return false; 
});
Ghommey
Thanks Ghommey... it did work for me.. but when i click on a div, it gets displayed properly, and when the second link is clicked, the first DIV's is still visible.. how do i hide the div of the links which are not clicked ??
Sullan
I added a working solution to my post.
Ghommey
Thanks Ghommey.. But my top links are also getting hidden when clicking on it ??
Sullan
You have to add another div surrounding the solution divs like I did in my post.
Ghommey
A: 

You're reusing ID attributes. IDs need to be unique to a document.

You can't have anchors and divs with the same ID.

Scottie
yeah i was so dumb to use the same attributes for the links and div's. Even after me changing the id to title, i am still not able to view the div's on click...
Sullan
A: 

This is my updated code

 $("a").live("click", function(){
 var idV = $(this).attr("title"); 
 $("#"+idV+".div").css("display","block");
 return false;
 });

and i have changed to the links to

<a href="#" title="solution1">Solution 1</a>

But still no luck... please help...

Sullan