Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-15-2011, 03:59 PM   PM User | #1
Partizan
Regular Coder

 
Partizan's Avatar
 
Join Date: Dec 2008
Location: Legoland and Ireland
Posts: 139
Thanks: 74
Thanked 0 Times in 0 Posts
Partizan is an unknown quantity at this point
Problem with onClick script

Hi,

Does anyone know how I can edit this script to meet my requirements?

The code below reveals the hidden text "Hello Joe" when the user clicks on the hyperlink "TEST-LINK1".

This works fine, however, I also would like previously revealed text (HELLO JOE) to be rehidden when the user clicks on another hyperlink, eg "TEST-LINK2" below which reveals the text "BYE JOE".

Any ideas on how to do this?

SCRIPT:

PHP Code:
<!-- ############# REVEAL HIDDEN TEXT ############# -->
<script language="javascript" type="text/javascript">
function 
displayDiv()
{
var 
dv document.getElementById("revealonclick");
dv.style.display = ( dv.style.display != "block" ) ? "block" "none";
}
</script>
<!-- ############# END REVEAL HIDDEN TEXT ############# --> 


BODY:
PHP Code:

<span onclick="displayDiv('revealonclick1'); return false;">TEST-LINK-1</span>
<
div id="revealonclick1">  

HELLO JOE

</div>


<
span onclick="displayDiv('revealonclick2'); return false;">
TEST-LINK-2
</span>

<
div id="revealonclick2">  

BYE JOE 

</div

CSS:
PHP Code:
#revealonclick{display:none;} 
#revealonclick1{display:none;} 
#revealonclick2{display:none;} 
#revealonclick3{display:none;} 
__________________
On the Internet, no one knows you’re a minifig…
Partizan is offline   Reply With Quote
Old 11-15-2011, 04:45 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
CSS
Code:
.HideMe {display:none;}
html
Code:
<span onclick="DisplayBox(1)"> Test One! </span>
<div id="BoxToBeDisplayed1" class="HideMe">
text
</div>

<span onclick="DisplayBox(2)"> Test Two! </span>
<div id="BoxToBeDisplayed2" class="HideMe">
text
</div>
javascript
Code:
<script type="text/javascript">
function DisplayBox(index)
{
HideAll();
var box = "BoxToBeDisplayed" + index;
document.getElementByID(box).style.display = "block";
}

function HideAll()
{
document.getElementByClass("HideMe").style.display="none";
}
</script>
should work but didn't test

Edit: Also should have been posted in javasript forum
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Users who have thanked alykins for this post:
Partizan (11-15-2011)
Old 11-15-2011, 05:10 PM   PM User | #3
Partizan
Regular Coder

 
Partizan's Avatar
 
Join Date: Dec 2008
Location: Legoland and Ireland
Posts: 139
Thanks: 74
Thanked 0 Times in 0 Posts
Partizan is an unknown quantity at this point
alykins, thank you for the feedback and your help. Also, apologies for not posting it in the Javascript section.

I am just trying to get it to work and have a few basic questions.

1. is this script meant to replace the script I had, or is it to be added to it?
2. do I need to add

#BoxToBeDisplayed1{display:none;}
#BoxToBeDisplayed2{display:none;}

to the css file, or something in addition to .HideMe {display:none;} ?

Thanks!
__________________
On the Internet, no one knows you’re a minifig…
Partizan is offline   Reply With Quote
Old 11-15-2011, 06:08 PM   PM User | #4
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
replaces all you had... let's go through the logic of it

onload all the boxes have class HideMe applied with display none, therefore no box will be displayed. On the click you send the index of the box you are targeting to the script function. The function fires, calls the other function (HideAll) which will go through and re-hide all boxes with that class (therefore hiding any shown boxes) then it will go through and un-hide the desired box

(*or at least it should *)
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Users who have thanked alykins for this post:
Partizan (11-16-2011)
Old 11-15-2011, 07:25 PM   PM User | #5
Partizan
Regular Coder

 
Partizan's Avatar
 
Join Date: Dec 2008
Location: Legoland and Ireland
Posts: 139
Thanks: 74
Thanked 0 Times in 0 Posts
Partizan is an unknown quantity at this point
alykins,

thanks for the explanation!

I have copied the code word for word but it is not yet working...

fyi, I have placed the script between HEAD tags.
__________________
On the Internet, no one knows you’re a minifig…

Last edited by Partizan; 11-16-2011 at 03:55 PM..
Partizan is offline   Reply With Quote
Old 11-16-2011, 05:06 AM   PM User | #6
tqmd1
New Coder

 
Join Date: Oct 2011
Posts: 53
Thanks: 9
Thanked 1 Time in 1 Post
tqmd1 is an unknown quantity at this point
Sir, did you see my this thread?

http://www.codingforums.com/showthread.php?t=243811

this is smiliar to current thread but i could not get proper solution.

Please guide me also.

Thanks
tqmd1 is offline   Reply With Quote
Users who have thanked tqmd1 for this post:
Partizan (11-16-2011)
Old 11-16-2011, 03:43 PM   PM User | #7
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
Quote:
Originally Posted by tqmd1 View Post
Sir, did you see my this thread?

http://www.codingforums.com/showthread.php?t=243811

this is smiliar to current thread but i could not get proper solution.

Please guide me also.

Thanks
yes i did see that link- and I also followed it- why you have so much preventative "security" around your source code is beyond annoying but seeing scripts that (on first glance didn't really peruse them) seem to try and search my cookies for user info I would suggest no one else go to this link-

Edit: Investigated it some more and it is behind a forum login thingy and the user id scripts and what not are not malicious; it is a required login site crap- can you post your code that you are trying in your thread- no one is helping because you have made it difficult to help
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE

Last edited by alykins; 11-16-2011 at 03:50 PM..
alykins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:36 AM.


Advertisement
Log in to turn off these ads.