use "getelementbyclass" to apply toggle layer script to specific class

Here's what I've got:
        <div class="right-header">
            <div class="poweredby-image"></div>
        </div>
I cannot modify the html above, but I can make a custom jscript available to the page as it draws. Via the script, I need to access the "poweredby-image" class and make it a toggle for showing/hiding a layer. No ID to work with, unfortunately. How can I do this? Please provide code.
GessWurkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Hello GessWurker,

Try : var e =  getElementByClassName("div", "right-header");

function getElementByClassName(tag, class) {
   var elements = document.getElementsByTagName(tag);
   for(var d=0;d<elements.length;d++) {
     if( elements[d].className == class ) { return elements[d]; }
   }
   return -1;
}

Open in new window

0
GessWurkerAuthor Commented:
leakim971: Thanks, but your suggestion is returning an error:  
"Expected identifier"
0
leakim971PluritechnicianCommented:
Try this one :

(and/or post part of your web page)


function getElementByClassName(tag, class) {
   var elements = document.getElementsByTagName(tag);
   for(var d=0;d<elements.length;d++) {
     try { 
        if( elements[d].className == class ) { return elements[d]; }
     }
     catch(e) {}
   }
   return -1;
}

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GessWurkerAuthor Commented:
same error: expected identifier. The page is inside a firewall, so can't show it to you.
One thing: Once we get this working, we should be able to test this way?

var e =  getElementByClassName("div", "right-header");
e.onclick=alert("you clicked me!")

?

0
rajapandian_81Commented:
try below one
(modified little bit)

var e =  getElementByClassName("div", "right-header");
alert(e);

function getElementByClassName(tag, classname) 
{ 
   var elements = document.getElementsByTagName(tag); 
   
   for(var d=0;d<elements.length;d++) 
   { 
     try 
     {  
        if( elements[d].className == classname ) { return elements[d]; } 
     } 
     catch(e) {} 
   } 
   return -1; 
}

Open in new window

0
GessWurkerAuthor Commented:
rajapandian:

Your suggestion is not causing errors, but still haven't been able to do what I want. I want to be able to click on the right-header div and fire an alert, i.e. alert("right-head clicked"). How can I do that?
0
leakim971PluritechnicianCommented:
@rajapandian_81 excellent!!! xlol
0
GessWurkerAuthor Commented:
Folks: I found a solution. I must credit http://www.tastypopsicle.com/getelementsbyclassname/

Here's what worked:

function changeInnerHTML(strTag,strClass,strText) {
  for (i=0;i<document.getElementsByTagName(strTag).length; i++) {
    if (document.getElementsByTagName(strTag).item(i).className == strClass) {
      document.getElementsByTagName(strTag).item(i).innerHTML = strText;
      }
    }
}

function adjContents() {
changeInnerHTML('div','right-header','<div class=\"poweredby-image\" style=\"background-image:url(../images/branding/toolbox.gif)\" onclick=\"alert(\'You clicked me!\')\"></div>');
}

setTimeout('adjContents()',3000);
0
rajapandian_81Commented:
@leakim971:  Thanks :)

@GessWurker:
              Your actual requirement is make a toggle for show/hide right?

var e =  getElementByClassName("div", "poweredby-image");
if (e.style.display == 'none')
    e.style.display = '';
else
    e.style.display = 'none';

function getElementByClassName(tag, classname) 
{ 
   var elements = document.getElementsByTagName(tag); 
   
   for(var d=0;d<elements.length;d++) 
   { 
     try 
     {  
        if( elements[d].className == classname ) { return elements[d]; } 
     } 
     catch(e) {} 
   } 
   return -1; 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GessWurkerAuthor Commented:
Thanks rajapandian 81!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.