I would like to have all of my paragraph text collapsed when the page loads. When I click on the heading, the paragraph text should expand. I have multiple headings (all ) that all contain on paragraph below them. This is part of a course I am doing and we have not learned JQuery yet so I need this to work without the use of JQuery. I have placed each of the blocks within a with the class="maincontent". See code snippet below:
<div class="maincontent">
<h2>How HowziTO came to be? </h2>
<p>
This website was created by me, a South African who moved to Toronto in my mid 20's. Through the move, I felt unprepared, and at times, alone. Although there are many sites that offer advice to new immigrants,
I thought it would be useful to create a site that is specific to South Africans, given our unique heritage.
So, if you feel like I felt, hopefull this website will help you settle in a little easier.
If you have any suggestions for the site, please feel free to reach out to me using the <a href="About.html">the about page</a>.
</p>
</div>
I would really appreciate any help. Thank you.
I have added a screen shot of my website below. From the screen shot, I would like the text under "How HowziTO Came to be?" to be collapsed until I click on the heading and then it expands. I would like it to work for the other two headings as well.
2
Answers
Thanks for all the help everyone. After searching through the options provided, I decided to use an onClick function. Below is my JS:
I had to add the following to my HTML:
and
as well as the following to my CSS
and
This allowed me to toggle the display on and off.
In your code, you can add ‘id’ to your p tag and pass the id to the onclick function handler on the h2 tag like this –
add
display: none;
to the p tag.And add JavaScript like this –
Every time you add a p tag add the element id to it and pass on the id to the expand function in the h2 tag. The function automatically closes the other tags and expands the required one.