us***@yahoo.com wrote on 20 mei 2007 in comp.lang.javascript
:
On May 19, 2:35 pm, Lee <REM0VElbspamt...@cox.netwrote:
>u...@yahoo.com said:
>Sorry, the code snippet should be:
<script type="text/javascript"><!--
function toggleRetry(val) {
var addr = document.getElementById('retry')
if (val) {
addr.style.display='';
addr.style.display='block';
is more logical since you had set the default style as:
>>><div id="retry" style="display:block">
but why not set the style to:
<div id="retry" style="display:none;">
and there should be no need for the onload in:
>>><body onload="toggleRetry(false);">
} else {
addr.style.display='none';
}
}
A short version:
function toggleRetry(val) {
document.getElementById('retry').style.display =
(val) ?'block' :'none';
}
>//--</script>
This bit of code:
<a href="#" onclick="toggleRetry(false)">the URL</a>.
tells the browser to execute your function and then, after
that onclick handler has finished, to load "#", which means
to reload the page and scroll to the top.
If you don't want to reload the page, your onclick handler
should return false.
onclick="toggleRetry(false);return false"
The "<!--" "//-->" comments serve no purpose, by the way.
You're not likely to come across any browsers that don't
know what the <scripttag means.
--
Hm...it is not working. I added <body onload="toggleRetry(false);">
but I got "addr has no properties" right after loading the page.
Show the error line.
And
with onclick="toggleRetry(false);return false", after clicking the
link, it still won't display the div...
Logical, my dear Watson, it should be:
onclick='toggleRetry(true);return false'
Why not try an even shorter version:
=====================================
<script type="text/javascript">
function toggleRetryOn() {
document.getElementById('retry').style.display =
'block';
}
</script>
<a href="#"
onclick="toggleRetryOn();return false;">Click me</a>
<div id='retry' style='display:none;'>
Display Retry
</div>
======================================
Finally using an anchor/link <a></ato execute a code function seems
illogical, as you would have to think about
the resulting page reloading if js is not available.
Use a button:
<button
onclick='toggleRetryOn();return false;'>
Click me</button>
Or a css pseudo-anchor:
<span
onclick = 'toggleRetryOn();return false;'
style = 'cursor:pointer;color:navy;text-decoration:underline;'
onmouseover = 'this.style.color="red";'
onmouseout = 'this.style.color="navy";'
>Click me</span>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)