Swaping link and text

S

SB

Hi
I have a floating div script but need a link to show and hide it on
our site. At present i can use the two links below

<p><a href="javascript:showMe();">Show mini basket</a>
<p><a href="javascript:hideMe();">Hide mini basket</a>

But it would be nice to have them combined in to one link that changes
properties and text on click. My Java/CSS is not great but any help
would be very much appreciated.

Simon B
 
R

Ronx

The method I use is to hide link2 when the <div> is hidden, and hide the
first link1 when the div and link2 are shown. This requires 3 <divs>
manipulated by JavaScript - one for each link and the 3rd for your
floating <div>.

http://www.rxs-enterprises.org/tests/anti-spam/hidden.asp uses this
technique (though the <div> concerned is not floating) - look for "View
the hidden fields" near the bottom of the page.
 
S

SB

The method I use is to hide link2 when the <div> is hidden, and hide the
first link1 when the div and link2 are shown.  This requires 3 <divs>
manipulated by JavaScript - one for each link and the 3rd for your
floating <div>.

http://www.rxs-enterprises.org/tests/anti-spam/hidden.aspuses this
technique (though the <div> concerned is not floating) - look for "View
the hidden fields" near the bottom of the page.

--
Ron Symonds - Microsoft MVP (FrontPage)
Reply only to group - emails will be deleted unread.

http://www.rxs-enterprises.org/fp

Many Thanks I'll have a play but I'm not confident in my ability to
get it working.
S.
 
S

SB

Many Thanks I'll have a play but I'm not confident in my ability to
get it working.
S.

Hi Ron thanks for your help but i'm still struggeling with this as i
can't follow what is going on in your example. I'm now tearing my hair
out. S.
 
T

Trevor Lawrence

SB,
I had a page in which I managed to do this

I thought I should reply earlier, but I saw Ron's so I didn't.

It is now dinner time here in AUS, but if you haven't cracked it by
tomorrow, I will try to send the HMTL code that you need
 
S

SB

SB,
I had a page in which I managed to do this

I thought I should reply earlier, but I saw Ron's so I didn't.

It is now dinner time here in AUS, but if you haven't cracked it by
tomorrow, I will try to send the HMTL code that you need
--
Trevor Lawrence
Canberra
Microsoft MVP - FrontPage
MVP Web Sitehttp://trevorl.mvps.org

Thanks Trevor
I thought this would be a simple problem but i can't seem to get to
grips with it. I'll try today but if i have no luck your code would be
a great help. many thanks
S.
 
J

Jon Spivey

Unless I'm missing something would it not be easier to just change the text
of the show/hide link from show div to hide div rather than showing and
hiding the links themselves? Something like this

<script type="text/javascript">
function showHide(){
if(!document.getElementById)return;
var a=document.getElementById('hiddenDiv'),
b=document.getElementById('hideLink');
a.style.display=(a.style.display=='none')?'':'none';b.innerHTML=(a.style.display=='none')?'show
div':'hide div';
}
</script>
<a id="hideLink" href="javascript:;" onclick="showHide();return false;">show
div</a>
<div id="hiddenDiv" style="display:none">
hidden text
</div>

Cheers,
Jon
 
T

Trevor Lawrence

Thanks Trevor
I thought this would be a simple problem but i can't seem to get to
grips with it. I'll try today but if i have no luck your code would be
a great help. many thanks
S.

Hi S,

I have looked at the further replies this morning and what Jon suggested
seems just like what I was going to send. i.e just show/hide the <div >
itself.

In case you need it, here is Jon's code again complete with the tags -<head>
<body> etc. - so that you can see what goes where. (I have made one small
change to show that the 2 states for style:display are 'none' and 'block'.)

<html>
<head>

<script type="text/javascript">
function showHide(){
if (!document.getElementById) return;

var a = document.getElementById('hiddenDiv'),
b = document.getElementById('hideLink');
a.style.display = (a.style.display=='none')?'block':'none';
b.innerHTML = (a.style.display=='none')?'show div':'hide div';}
</script>

</head>

<body>
<a id="hideLink" href="javascript:;" onclick="showHide();return false;">
show div</a>

<div id="hiddenDiv" style="display:none">
hidden text
</div>

</body>
</html>

BTW,
1. Ron's reference (which is a lot more code) suggests that the <div> can
also be a floating <div>. But this may be a bit of overkill since Jon's code
works fine.
2. If it helps understand what the function is doing, it can be written as
function showHide(){
if(!document.getElementById) return;

var a=document.getElementById('hiddenDiv'),
b=document.getElementById('hideLink');

if (a.style.display=='none')
{ a.style.display='block'; b.innerHTML='hide div'}
else
{ a.style.display='none'; b.innerHTML= 'show div'}
}

i.e.
if the display of 'hidden Div' is 'none',
then it is set to 'block' and the text is changed to 'hide div'
else it is set to 'none' and the text is changed to 'show div'
 
J

Jon Spivey

In case you need it, here is Jon's code again complete with the
tags -<head> <body> etc. - so that you can see what goes where. (I have
made one small change to show that the 2 states for style:display are
'none' and 'block'.)

It's best to set style.display to '' (empty string) when you want to display
something -
style.display=''
and
style.display='block'
will both do the same thing on a div which is a block level element - the
empty string sets display to it's default for the element ie block or
inline. If you used my script with style.display='block' to show/hide
something that isn't a block element (eg an image etc) it might have
unexpected results - better not to worry about it and use an empty string
which will always work on anything. I'm a great believer in writing scripts
that can be used over and over without change ;-)

Cheers,
Jon
 
T

Trevor Lawrence

Jon Spivey said:
It's best to set style.display to '' (empty string) when you want to
display something -
style.display=''
and
style.display='block'
will both do the same thing on a div which is a block level element - the
empty string sets display to it's default for the element ie block or
inline. If you used my script with style.display='block' to show/hide
something that isn't a block element (eg an image etc) it might have
unexpected results - better not to worry about it and use an empty string
which will always work on anything. I'm a great believer in writing
scripts that can be used over and over without change ;-)

Cheers,
Jon

Thanks, Jon
That makes good sense.

I agree that is a good principle to keep things simple and re-usable. Trying
to be too exact (as I guess I was) can get one into problems. And the OP may
not know (or want to know) the difference between a block and inline element
anyway.

The only one small problem (not a criticism by any means) is that we need to
make sure that '' can be distinguished from ", especially when posting in a
newsreader (OE in may case).
 
S

SB

Thanks, Jon
That makes good sense.

I agree that is a good principle to keep things simple and re-usable. Trying
to be too exact (as I guess I was) can get one into problems. And the OP may
not know (or want to know) the difference between a block and inline element
anyway.

The only one small problem (not a criticism by any means) is that we need to
make sure that '' can be distinguished from ", especially when posting in a
newsreader (OE in may case).
--
Trevor Lawrence
Canberra
Microsoft MVP - FrontPage
MVP Web Sitehttp://trevorl.mvps.org

Wow thanks so much for all your help. I have it working on a test page
and will get it up on our test skin tomorrow so we can try cookie the
state on each user session.

Thanks All

Simon B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top