Jump to content

how to use 1 javascript function for 2 div or more ?


Recommended Posts

I have the following html code, what can i do in it so that by using only one javascript function, I can use it for the following div: box, box2, boxa2, boxb3
html at - www.jsfiddle.net/cAtFu/
Link to comment
Share on other sites

It isn't clear to me what you want to do. Perhaps something like...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>title</title><style>#box1{      background-color:yellow;}#box2{    visibility:hidden;    background-color:orange;}</style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>function showhide(show,hide){   //alert(show+':'+hide);   hidebox = document.getElementById(hide);   hidebox.style.visibility="hidden";       unhidebox = document.getElementById(show);   unhidebox.style.visibility="visible";}</script></head><body><span id="box1">    <a href="#" onclick="showhide('box2','box1')">Show box2 Hide box1</a></span><span id="box2">    <a href="#" onclick="showhide('box1','box2')">Show box1 Hide box2</a></span></body>    </html>
Link to comment
Share on other sites

I've done some work on this...



I believe this is what you are looking for. I'm not 100% sure either as you aren't completely clear, but I think I nailed it.


I changed a few things here:

1) removed the iframes, we don't need to be working with them to understand this exercise, and it really bottlenecks the loading times on my end.

2) changed from using visibility to using display, I think this is why you began to mess with positions when you didn't need to

3) you don't need to use anchor tag's href attribute to run javascript, its not 1997 and no one uses Netscape anymore. Present-day browsers are far more flexible so you can have pretty much any element run javascript via specific events.

4) cleaned up the css to help illustrate which divs are which.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...