1

I would be forever grateful if someone could help me out with this...

I've been trying to implement this feature for 4 days now, no joke.. I have researched everywhere, tried everything, and I finally accept that I'm defeated.

I know this is probably an incredibly simple javascript/jQuery function, but I can't get any of the functions I've tried to work within Visual Composer

Important Info: I'm using Visual Composer inside wordpress. Visual Composer allows you to add classes to elements (divs), but not id's. Visual Composer has a "raw javascript" element, a "raw html" element, and a separate section to enter CSS.

Setup: I have two images (div Aa and Ab) which are links to a separate part of the page. I also have two sections of content (div Ba and Bb).

What I am trying to do: div Aa and Ab should be hidden by default. When div Aa is clicked, Ba appears and Bb stays hidden. When div Ab is clicked, Bb appears and Ba stays hidden. If Bb is already visible and Aa is clicked, Bb disappears while Ba appears, and vice versa

I've come pretty close with this script:

HTML

<div id="Aa">Image #1</div>
<div id="Ab">Hidden Content #1</div>
<div id="Ba">Image #2</div>
<div id="Bb">Hidden Content #2</div>

CSS

#Ab{display:none;}
#Bb{display:none;}

Javascript

$('#Aa').click(function() {
$('#Ab').show();
});

$('#Ba').click(function() {
$('#Bb').show();
});

However, I can't get it to work in Visual Composer (VC) for the life of me! I've tried changing all the "#" to "." to refer to the class names VC forces me to use. I've tried changing all the "$" to "jQuery" as I read somewhere wordpress needs it formatted that way. I tried adding the <script></script> tags around it, and calling (?) the function such like <script src="">... Nada.

JipJipJimmy
  • 23
  • 2
  • 8

1 Answers1

2

You can use css id in row or inner in the visual composer. Well, you can use class in elements under the extra class element or inner column.

For suppose, first you select the single image and type "Aa" (without quotes as well without "." period) in the extra class like below image:

enter image description here

Now select another element text block and type class "Ab", create two other elements or copy paste and change the classes "Ba", "Bb" vice versa.

Now you have four elements two single image and two text block. Add one more element Raw JS from structure tab and paste following jquery code.

<script>
jQuery(document).ready(function(){
 jQuery('.Aa').click(function() {
    jQuery('.Ab').show();
});
 jQuery('.Ba').click(function() {
    jQuery('.Bb').show();
});
});
</script>

Add CSS to visual composer, click gear icon on top. (see image):

enter image description here

Paste below css code:

.Ab,
.Bb {
    display: none;
}

.Aa,
.Ba {
    cursor: pointer;
}

That's end, now you click on image, text block will show. Here is back-end look.

enter image description here

Here is front-end look before click.

enter image description here

And here is fornt-end look after click.

enter image description here

Also other ways to accomplish, using wp_enqueue_script and wp_equeue_style hook to add js and css in wordpress. Hope, this help you.

Nand Lal
  • 682
  • 1
  • 11
  • 25
  • I get what you're trying to do here, but the script just wont trigger. I tried implementing it just has you suggested, had to change it around a little because your CSS caused both the image and text to be hidden by default. Additionally, the display: none property wouldn't work, I had to use visibility: hidden. Do you think there could be an issue with my plugin?? – JipJipJimmy Feb 05 '17 at 01:46
  • @JipJipJimmy It's working fine my side, and this this practical. Please check your browser's console log. My CSS not hidden both image and text, both are seperate classes, make sure you are using css class on elements or on column/row. I am using classes in each element. – Nand Lal Feb 05 '17 at 09:40
  • Lai I figured out the problem!!! I have a VC extension called One Page which allows you to "name" rows and choose those names as an "on click" actions for elements, so when clicked, the page scrolls smoothly down to the desired row. When I have this implemented on say an image, your script doesn't work (conflicting onclick codes?). However, with what I'm trying to do I need the scrolling because when Aa image is clicked, page should scroll to and show Ba, and vice versa for Bb. I also I need the alternative to hide when the other is clicked. Anyway you can further help me out with this?? – JipJipJimmy Feb 07 '17 at 11:38
  • I DID IT!!! I got the entire functionality I was looking for to work. I pieced together your code with an "animate/scroll top" code and removed the OnePage extension all together. THANK YOU SO MUCH for all your help!!! – JipJipJimmy Feb 07 '17 at 12:25