Jump to content
Sign in to follow this  
atulthakre

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/

Share this post


Link to post
Share on other sites

So that it does what?

in my html code i have 1 jascript and 4 div (box.box2,boxa2,boxb3)i want to use that one javascript for 4 div(box.box2,boxa2,boxb3) how can i do that ?

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

I've done some work on this...

http://jsfiddle.net/cAtFu/9/

 

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...