tags:

views:

91

answers:

5

Hi all, I have this jquery script which suppose to hide/show div element base on the hyperlink that is clicked. I don't know why but it's not working at all. The following is the except of my code.

function hide_current_commoncontainer(commoncontainer){
   $(commoncontainer).each(function(){
 if(this.is(":visible")){this.hide();} 
 });
}

function init(){
$("#compose_link").bind("click",function(){
 hide_current_commoncontainer(".pmcommoncontainer");
 ("#composer").show();  
 return false;
 }
);

if(Drupal.jsEnabled){  
 $(document).ready(init);
} 

I've pinpoint the cause and found out that it is show/hide function which is not working. The rest - function calls - are ok. Can anyone tell me where I am doing wrong? Where should I amend my code in order to hide/show div element as the way I wanted.

A: 

You're missing the $ before ("#composer").show(); and this

Shouldn't it be $("#composer").show(); and $(this)? Also, make sure you've pulled in the definitions of show/hide

psychotik
actually, there is $ sign before ("#composer") in actual code. I think it was deleted accidentally when I edit the post. I was having hard time when I tried to high light the code cuz I'm new to this forum.Anyway, I added the $ sign to "this" and it does hide the element but still $("#composer").show() doesn't show composer element.On a side note, I must say woow super fast response. Never experience this kind of fast response in any other forum. thanks for the reply.
Andrew
This form works because of the achievements - if you see an answer that helps, you 'up-vote' it and if I find an answer you mark it as the answer. Please do your part to keep it as responsive. Thx.
psychotik
A: 

instead of this, you have to use $(this) for jquery objects.

contagious
Yeap I added it and it does hide the element that i wanted to hide but still $("#composer").show() doesn't work.
Andrew
+1  A: 

I think that there are a couple of areas that need to be tweaked in your code sample.

REPLACE

("#composer").show();  

WITH

$("#composer").show();  


REPLACE

if(this.is(":visible")){this.hide();} 

WITH

if($(this).is(":visible")){$(this).hide();} 
RSolberg
I did add $ sign in front of this and that part works now. But, as for composer element, it was a mistake on the posted code only(accidentally deleted during editing), the actual one does have it. So show still doesnt work.
Andrew
+1  A: 

I'd suggest you do something like

$(function() {
    $('#compose_link').click(function(e) { 
        $('.pmcommoncontainer').children(':visible').hide();
        $('#composer').show();
        e.preventDefault();
    });
});

Which will show the element with the id of composer, and hide all visible child elements of the class pmcommoncontainer when the element with the id compose_link is clicked.

I think this is what you want to do - no need to iterate as jQuery works with sets :).

EDIT

Looks like you also wanted the click or post to be stopped - with events you can use the preventDefault() function to stop that behaviour.

Khanzor
+1: I like your solution a bit better than simply `fixing` the sample...
RSolberg
I'm not sure what you are trying to say in the last line but, just to give you a bit of scenario, I have this bunch of hyperlinks on my page and each link will display corresponding div that has child elements in it, at the very same spot. And, on the way, it suppose to hide the div which is already on display before it shows the corresponding div. So, in order to do that, should I also hide/show the child element or would it be enough to show/hide parent div only.
Andrew
Ok, I just adapted mine to your code and still composer element doesn't show up. :confused:
Andrew
A: 

Ok I found out why $("#composer").show() is not working. It's because I hard-coded the visibility style of those divs to "hidden" and jquery's "show" method can't revert that. Strangely, as opposed to "show" method, "hide" can revert hard-coded "visible" style with no problem. So, to hide/show elements as intended, I either have to use hide/show method combo without no visibility style hard-coding or use css method of jquery and set the visibility style as desire.

Andrew