Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder Grobulous's Avatar
    Join Date
    Sep 2006
    Location
    Earth...
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts

    replace class of element with anchor click

    the title pretty much sums it up I guess. I hope this is in the right place.

    Anyhow, what I want is a link with an onClick attribute that changes the class attribute of an element on the page, using some kind of javascript function. Now, I'm new at javascript, so I can't really write one on my own, so if anyone can help and point me in the right direction or give me some samle code or something, that would be cool.

    I've been searching online for a while now, so I am about to give up.

    But here is an example of what I was talking about, if you need html for reference or something...

    Code:
    <a href="#" onclick="whateverthefunctionis"><img src="someimage" alt="someimage" class="small" /></a>
    <p><a href="#" onclick="whateverthefunctionis">clicking here changes the class</a></p>
    when you click the image when it is 'small', it changes it to 'orig'. when you click it when it is 'orig' it changes it to 'small'.

    I hope someone can help. It's probably real simple but I can't figure it out.
    Release your mighty juices of creativity... SabuKudo.com!

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Try something like this:
    Code:
    <script type="text/javascript">
    function toggle(elemId) {
      imgElem = document.getElementById(elemId);
      imgElem.className = (imgElem.className == 'small') ? 'orig' : 'small';
    }
    </script>
    
    <img id="myimage" src="zebra.jpg" alt="someimage" class="small" onclick="toggle('myimage')"/>
    <span onclick="toggle('myimage')">toggle the class</span>

  • #3
    New Coder Grobulous's Avatar
    Join Date
    Sep 2006
    Location
    Earth...
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks! that helped a lot!
    Release your mighty juices of creativity... SabuKudo.com!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •