W
webmasterATflymagnetic.com
I would be grateful for some pointers to the code below. I want to
click the button and it toggles the showing of text. The state is
tracked in the variable det. Whilst the function itself works, I have
two questions:
* Why does it not 'stick' with the new value (ie det = true), but keep
reverting to the false value?
* Why does it work even more poorly in IE? The line with
getElementById() in particular seems not to rewrite the HTML.
I expect it to start with det = false, so only show the "Detail"
button. When you click this you should then
see the info text, and a new button with the text "Close". When this
is showing det should be true. The idea is to keep toggling between
the two states as you click the button.
Is there some fundamental error with this? If so I can't see it.
<html>
<head>
<script>
var det = false;
var info = "This is some information to show.";
function toggleDetail() {
var text;
det = !det; // Toggle detail: doesn't see to work.
if(det) {
text = info + " " + detailLink("Close")
}else{
text = " " + detailLink("Details");
}
alert(det);
document.getElementById("detail").innerHTML = text;
return true;
}
function detailLink(text) {
return "<input type = \"submit\" value = \"" + text + "\">"
}
</script>
</head>
<body>
<h1>Heading</h1>
<p><form onSubmit="toggleDetail();">
<span id = "detail"><input type="submit" value="Details"></span></p>
</form>
</body>
</html>
click the button and it toggles the showing of text. The state is
tracked in the variable det. Whilst the function itself works, I have
two questions:
* Why does it not 'stick' with the new value (ie det = true), but keep
reverting to the false value?
* Why does it work even more poorly in IE? The line with
getElementById() in particular seems not to rewrite the HTML.
I expect it to start with det = false, so only show the "Detail"
button. When you click this you should then
see the info text, and a new button with the text "Close". When this
is showing det should be true. The idea is to keep toggling between
the two states as you click the button.
Is there some fundamental error with this? If so I can't see it.
<html>
<head>
<script>
var det = false;
var info = "This is some information to show.";
function toggleDetail() {
var text;
det = !det; // Toggle detail: doesn't see to work.
if(det) {
text = info + " " + detailLink("Close")
}else{
text = " " + detailLink("Details");
}
alert(det);
document.getElementById("detail").innerHTML = text;
return true;
}
function detailLink(text) {
return "<input type = \"submit\" value = \"" + text + "\">"
}
</script>
</head>
<body>
<h1>Heading</h1>
<p><form onSubmit="toggleDetail();">
<span id = "detail"><input type="submit" value="Details"></span></p>
</form>
</body>
</html>