Solved

how to display or hide an image based upon javascript variable

Posted on 2009-05-14
2
257 Views
Last Modified: 2013-12-07
Trying to properly test GWO.. all pages and data are dynamic with URL rewites.  I want to be able to display or hide images for google website optimizer testing

My idea is to have this javascipt
<script type="TEXT/JAVASCRIPT">var display=1
</script>
which i can then not be used in a GWO option

then later when the images are displayed they are hidden if display is not 1

the image is shown by a function call
tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT,parameters)

parameters can be style="display: none;" which hides the image... I know how to do this with an onclick or other event but do not know how to show or hide the image (or text block or whatever) based on whether a javascript variable is set earlier

Though this question is specific it is general in that I am looking for a way to display or hide  a php produced html block with javascript so I can perform GWO tests
0
Comment
Question by:levelninesports
2 Comments
 
LVL 7

Accepted Solution

by:
tg_wilk earned 500 total points
ID: 24391311
If you set all images to display: none at the beginning, you can iterate through all images at the bottom of the page and set display to inline or block, if display variable is set:
<script type="text/javascript">
if (display == 1)
{
    var images = document.getElementsByTagName("img");
    for(var j=0; j<images.length; j++)
    {
       if (images[j].style.display == 'none') images[j].style.display='inline';
    }
}
</script>

Open in new window

0
 

Author Closing Comment

by:levelninesports
ID: 31581747
thanks worked great.. just for completeness the image display line read
tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT,'id="img" style="display: none;"')
the end part being the relevant piece
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question