I made the following. I want to click on list item to toggle next div.
JSFiddle Demo
When click
on list item
,
<li><a href="#">Create/Read/Manipulate</a></li>
next div.submenu
display, again click to hide to reflect toggle like effect in jQuery.
<div class="submenu">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div>
I do not want to use jQuery. Pure CSS or JavaScript is wele.
#leftcolumn {
width: 18%;
margin: 3px;
padding: 0;
display: inline;
}
#leftcolumn ul.leftmenu {
list-style: none;
text-align: left;
margin: 0 5px 3px 10px;
padding: 0px;
}
#leftcolumn ul.leftmenu li {
border-bottom: 1px solid #EFF0F1;
font-size:14px;
padding: 1px 0px;
}
#leftcolumn ul.leftmenu li a {
line-height: 20px;
color: #333;
text-decoration: none;
}
#leftcolumn ul.leftmenu li a:hover {
color: #FA7837;
}
#leftcolumn ul.leftmenu li#activemenu a {
color: #FA7837;
}
#leftcolumn ul.leftmenu .submenu {
margin: 3px 0 3px 7px;
}
#leftcolumn ul.leftmenu .submenu a {
color: #333;
font-weight: normal;
font-size:14px;
text-decoration: none;
line-height: 1.3em;
}
#leftcolumn ul.leftmenu .submenu a:hover {
color: #F9864D;
text-decoration:underline;
}
#leftcolumn ul.leftmenu .submenu a#activesubmenu {
color: #F9864D;
}
<table id="maintable" cellspacing="0" cellpadding="0">
<tr>
<td id="leftcolumn" >
<ul class="leftmenu">
<li><a href="#">CDATA</a></li>
<li><a href="#">Create/Read/Manipulate</a></li>
<div class="submenu">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div>
<li><a href="#">Entity Declaration</a></li>
<li><a href="#">Create/Read/Manipulate</a></li>
<div class="submenu">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div>
</ul>
</td>
</tr>
</table>
I made the following. I want to click on list item to toggle next div.
JSFiddle Demo
When click
on list item
,
<li><a href="#">Create/Read/Manipulate</a></li>
next div.submenu
display, again click to hide to reflect toggle like effect in jQuery.
<div class="submenu">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div>
I do not want to use jQuery. Pure CSS or JavaScript is wele.
#leftcolumn {
width: 18%;
margin: 3px;
padding: 0;
display: inline;
}
#leftcolumn ul.leftmenu {
list-style: none;
text-align: left;
margin: 0 5px 3px 10px;
padding: 0px;
}
#leftcolumn ul.leftmenu li {
border-bottom: 1px solid #EFF0F1;
font-size:14px;
padding: 1px 0px;
}
#leftcolumn ul.leftmenu li a {
line-height: 20px;
color: #333;
text-decoration: none;
}
#leftcolumn ul.leftmenu li a:hover {
color: #FA7837;
}
#leftcolumn ul.leftmenu li#activemenu a {
color: #FA7837;
}
#leftcolumn ul.leftmenu .submenu {
margin: 3px 0 3px 7px;
}
#leftcolumn ul.leftmenu .submenu a {
color: #333;
font-weight: normal;
font-size:14px;
text-decoration: none;
line-height: 1.3em;
}
#leftcolumn ul.leftmenu .submenu a:hover {
color: #F9864D;
text-decoration:underline;
}
#leftcolumn ul.leftmenu .submenu a#activesubmenu {
color: #F9864D;
}
<table id="maintable" cellspacing="0" cellpadding="0">
<tr>
<td id="leftcolumn" >
<ul class="leftmenu">
<li><a href="#">CDATA</a></li>
<li><a href="#">Create/Read/Manipulate</a></li>
<div class="submenu">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div>
<li><a href="#">Entity Declaration</a></li>
<li><a href="#">Create/Read/Manipulate</a></li>
<div class="submenu">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div>
</ul>
</td>
</tr>
</table>
Share
Improve this question
edited Oct 28, 2014 at 11:54
Hashem Qolami
99.6k27 gold badges155 silver badges165 bronze badges
asked Oct 28, 2014 at 10:56
Jaykumar PatelJaykumar Patel
27.7k12 gold badges78 silver badges76 bronze badges
7
- The Code Snippet is used for putting the code here, so that even that gets versioned. Do not use jsFiddle. – Praveen Kumar Purushothaman Commented Oct 28, 2014 at 11:04
-
You should not use a
div
as direct child oful
. Rather keep thediv.submenu
in the submenu's parentli
. Only ali
should be the direct child oful
andol
– Anis Commented Oct 28, 2014 at 11:18 - 1 PLEASE DO NOT UPDATE QUESTION only minor reason using snippet editor instead of jsfiddle. Is this not a issue. please. – Jaykumar Patel Commented Oct 28, 2014 at 11:23
- Okay, I give up @user1153551. Don't worry. I am not editing it. – Praveen Kumar Purushothaman Commented Oct 28, 2014 at 11:26
- You should put the relevant code to the question so that it could stand by itself. meta.stackexchange./questions/114942/… – Hashem Qolami Commented Oct 28, 2014 at 11:46
4 Answers
Reset to default 5You can use hover.
#leftcolumn {
width: 18%;
margin: 3px;
padding: 0;
display: inline;
}
#leftcolumn ul.leftmenu {
list-style: none;
text-align: left;
margin: 0 5px 3px 10px;
padding: 0px;
}
#leftcolumn ul.leftmenu li {
border-bottom: 1px solid #EFF0F1;
font-size:14px;
padding: 1px 0px;
}
#leftcolumn ul.leftmenu li a {
line-height: 20px;
color: #333;
text-decoration: none;
}
#leftcolumn ul.leftmenu li a:hover {
color: #FA7837;
}
#leftcolumn ul.leftmenu li#activemenu a {
color: #FA7837;
}
#leftcolumn ul.leftmenu #submenu {
margin: 3px 0 3px 7px;
}
#leftcolumn ul.leftmenu #submenu a {
color: #333;
font-weight: normal;
font-size:14px;
text-decoration: none;
line-height: 1.3em;
}
#leftcolumn ul.leftmenu #submenu a:hover {
color: #F9864D;
text-decoration:underline;
}
#leftcolumn ul.leftmenu #submenu {
display: none;
}
#leftcolumn ul.leftmenu li:hover #submenu {
display: block;
}
#leftcolumn ul.leftmenu #submenu a#activesubmenu {
color: #F9864D;
}
<table id="maintable" cellspacing="0" cellpadding="0">
<tr>
<td id="leftcolumn" >
<ul class="leftmenu">
<li><a href="#">Syntax/Rules</a></li>
<li><a href="#">Data</a></li>
<li><a href="#">CDATA</a></li>
<li><a href="#">Create/Read/Manipulate</a>
<div id="submenu">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div></li>
<li><a href="#">Entity Declaration</a></li>
<li><a href="#">How To Write</a></li>
</ul>
</td>
</tr>
</table>
As you specifically mentioned using the 'click' event:
Your corrected HTML (only <li>
elements are valid children of a <ul>
or <ol>
element):
<li>
<input id="demo" type="checkbox" />
<label for="demo">Create/Read/Manipulate</label>
<div id="submenu"> <a href="#">DOM</a>
<br /> <a href="#">Reader/Writer</a>
<br /> <a href="#">SimpleXML</a>
<br />
</div>
</li>
And additional CSS:
/* to emulate the 'feel' of the other <a> elements: */
label {
cursor: pointer;
}
/* move the checkbox outside of the visual field to hide it: */
li input[id][type="checkbox"] {
position: absolute;
left: -1000px;
top: -1000px;
}
/* styling <div> elements with an [id] attribute that are a subsequent
sibling of an <input> with an [id]: */
input[id] ~ div[id] {
display: none;
}
/* styling the subsequent sibling <div> element of a :checked <input>
that has an [id]: */
input[id]:checked ~ div[id] {
display: block;
}
JS Fiddle demo.
This employs the ability of <input />
elements, of types checkbox
and radio
to preserve a state outside of :hover
events, and use of the :checked
pseudo-class to style elements based upon that state.
Bringing the relevant <div>
element inside of the <li>
then allowed use of the general-sibling binator (the ~
) to target that <div>
based on the state (:checked
/unchecked) of the checkbox.
I found two different solution,
1.) Use <input/>
type checkbox,
li input[id][type="checkbox"] {
position: absolute;
left: -1000px;
top: -1000px;
}
input[id] ~ div[id] {
display: none;
}
input[id]:checked ~ div[id] {
display: block;
}
View demo jsFiddle
2.) Open submenu, when mouse hover,
#leftcolumn ul.leftmenu #submenu a:hover {
color: #F9864D;
text-decoration:underline;
}
#leftcolumn ul.leftmenu #submenu {
display: none;
}
#leftcolumn ul.leftmenu li:hover #submenu {
display: block;
}
#leftcolumn ul.leftmenu #submenu a#activesubmenu {
color: #F9864D;
}
View demo jsFiddle
Check Out this, This is will work. use,
<li><a href="#" onclick="enableDiv();">Create/Read/Manipulate</a></li>
<style>
#leftcolumn {
width: 18%;
margin: 3px;
padding: 0;
display: inline;
}
#leftcolumn ul.leftmenu {
list-style: none;
text-align: left;
margin: 0 5px 3px 10px;
padding: 0px;
}
#leftcolumn ul.leftmenu li {
border-bottom: 1px solid #EFF0F1;
font-size:14px;
padding: 1px 0px;
}
#leftcolumn ul.leftmenu li a {
line-height: 20px;
color: #333;
text-decoration: none;
}
#leftcolumn ul.leftmenu li a:hover {
color: #FA7837;
}
#leftcolumn ul.leftmenu li#activemenu a {
color: #FA7837;
}
#leftcolumn ul.leftmenu #submenu {
margin: 3px 0 3px 7px;
}
#leftcolumn ul.leftmenu #submenu a {
color: #333;
font-weight: normal;
font-size:14px;
text-decoration: none;
line-height: 1.3em;
}
#leftcolumn ul.leftmenu #submenu a:hover {
color: #F9864D;
text-decoration:underline;
}
#leftcolumn ul.leftmenu #submenu a#activesubmenu {
color: #F9864D;
}
</style>
<script>
function enableDiv()
{
var e = document.getElementById("submenu");
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
<table id="maintable" cellspacing="0" cellpadding="0">
<tr>
<td id="leftcolumn" >
<ul class="leftmenu">
<li><a href="#">Syntax/Rules</a></li>
<li><a href="#">Data</a></li>
<li><a href="#">CDATA</a></li>
<li><a href="#"
onclick="enableDiv();">Create/Read/Manipulate</a></li>
<div id="submenu" style="display:none;">
<a href="#">DOM</a><br />
<a href="#">Reader/Writer</a><br />
<a href="#">SimpleXML</a><br />
</div>
<li><a href="#">Entity Declaration</a></li>
<li><a href="#">How To Write</a></li>
</ul>
</td>
</tr>
</table>