tags:

views:

18

answers:

1

I have following code created dynamically with php.

What I want to do is when I click class='toggle' with href of desc76, I want to show ul with id desc76.

also I want to change arrowdown16.png to arrowup16.png.

Then when I click it, ul will fadeOut or hidden again.

class, secondul has CSS display:hidden;.

I tried this, but it does not work.

 $(".toggle").click(function () {
    event.preventDefault();
    var id = "#"+$(this).attr('href')
     $("id").show("slow");
    });

Thanks in advance for you help.

<ul>
...
...
<li class='toggledetail'>
Show Details
<div>
<a href='desc76' class='toggle' >
<img src="http://blablabla.com/assets/icons/arrowdown16.png"  alt="arrowdown16"  title="Show Details" /></a>
</div>
</li>
</ul>

<ul class='secondul' id='desc76'>
<li class='listdetail'>Spec Details<div>0</div>
</li>
</ul>
<li class='toggledetail'>
Show Details<div>
<a href='desc75' class='toggle' >
<img src="blablabla.com/assets/icons/arrowdown16.png"  alt="arrowdown16"  title="Show Details" /></a>
</div>
</li>
</ul>
<ul class='secondul' id='desc75'>
<li class='listdetail'>Spec Details<div>0</div>
</li>
</ul>

... Here comes more ul
...
+1  A: 

Updated for image:

$(".toggle").live('click', function () {
    event.preventDefault();
    var id = "#"+$(this).attr('href');
    // change image of the next element
    $(this).next('img').attr('src', 'path here');
    $("id").show("slow");
});

Note if you want to show/hide the element with click on the same element, you need to use slideToggle instead of show. Or you can use the animate method with opacity:'toggle' for fadding toggle effect.

You need to use live() method for dynamically generated elements:

$(".toggle").live('click', function () {
    event.preventDefault();
    var id = "#"+$(this).attr('href');
    $("id").show("slow");
});
Sarfraz
Thanks. I just updated my post. I actually want to toggle to show and hide and change image. Thanks for your help.
shin
i dont think he means that kind of dynamically like AJAX. he just means the content is made with PHP. not a static HTML page. live wont help.
Stefanvds
+1 for a brave attempt at decrypting the question with a workable solution =)
David Thomas
Yes, content is created by php not ajax.
shin
@shin: See my updated please.
Sarfraz
@Stefanvds: Either way, the `live` will work. I just posted just in case :)
Sarfraz
I added event in .live('click', function(event) to work. However it still does not display. It could be with html/css?
shin
$("id").show("slow"); should be $(id).show("slow");then it works.
shin