100 likes | 258 Views
Using Javascript to Hide and Show Content. Web Design Sec 6-6. Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development I” Course materials. Objectives. The student will: Be able to use CSS to hide and display content.
E N D
Using Javascript to Hide and Show Content Web Design Sec 6-6 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development I” Course materials
Objectives • The student will: • Be able to use CSS to hide and display content. • Be able to use JavaScript to change and HTML element's CSS properties.
Overview • It's common practice on today's web to hide content from users until it's needed. • JavaScript is commonly used is to hide or display content based on user behavior. • For example, a user may select an option when filling out an on-line form, and their selection might cause other related form fields to appear.
Using CSS Code to Show/Hide Content • The display property is used to show or hide content. • Adding display: none; to the CSS code is one of several ways to hide content using CSS. • Changing the code to display: block; will make that element visible again.
Using Javascript to Change the display element • You have learned that you can use Javascript to create HTML code. • Javascript can also be used to change CSS code or even check the setting of CSS code.
Setting the Display Status in CSS Code • To change the setting for the display element for my clock, first I need to read the current settings. varmyClock= document.getElementById('clock'); • Now I can alter the display setting by: //Turn it off myClock.style.display= 'none'; //Turn it on myClock.style.display = 'block';
Rest of Today • Download the homework for today. • Add the button to toggle your clock on and off. • Note: you also change the content of the button used to toggle the clock!