i’m struggling with display/hide text using javascript here.
Here is what i wanted to achieve like the picture below:

and the below is my javascript i’ve got:
function change_display(display)
{
if(display.style.display=="none")
{
display.style.display="";
}
else
{
display.style.display="none";
}
}
Html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="Style_sheet.css" rel="stylesheet" type="text/css" />
<script src="Display text.js" type="text/javascript"></script>
</head>
<body onload="change_display(display)">
<div class="Body">
<div class="header">
<h1 id="head">Manage Components</h1>
<h3 id="select-system">Select System</h3>
</div>
<div class="side-nav">
<a href="javascript:change_display(display)">192.101 English A</a>
<div id="display"><a href="javascript:change_display(display)"> Section 1:</a></div>
<div id="display"> Topic 1:</div>
</div>
</div>
</body>
</html>
What i’ve got there doesn’t work. I don’t know how to get it to work
Although, i could display as text 1 but if i want to keep click and display downward, it won’t work.
Oh also, i want use css style with it as well, is there anyway i could do? cos i already use ID for javascript therefore, i can’t use ID again for css. I’ve tried to use “name” but doesn’t work at all.
Please help! Thanks in advance!
A couple of the obvious issues:
you are calling
change_display(display), but you have not created/filled a variable nameddisplay. you can’t have multiple elements with the same id. you can’t directly access an html element by id- you have to get a reference to it using the id.some minor things we’ll clean up right away:
the call to
change_displayis not needed forbody‘sloadevent. calling js fromhrefis bad form.javascript: