I have this working code for a toggle-item inside a Bootstrap dropdown-menu. See demo here.
I suspect that it should be possible to improve it :
in order to have a shorter code and avoid all the
(...).css('visibility','hidden');
code redudancy... Is it really like this that I should move thetick
icon on the selected item of thePrivate/Public
menu?are the inline HTML
style="..."
a problem here and there ?more generally, as I learned JS by myself (+ with help of people on SO as well!), are there some good pratice that I should use here and there in this code ? Naming for the class/id ? Replace the use of class by id ? some other things ?
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<style>
#privacy-button {
margin: 3px 0 10px 20px;
line-height: 10px;
}
#privacy-menu {
margin-top:-15px;
margin-left:20px;
}
</style>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<ul class="nav navbar-nav">
<li><a href="#">Home</a> </li>
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Settings<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" style="min-width: 210px;">
<li><a href="#">Rename</a> </li>
<li><a href="#">Delete</a> </li>
<li class="dropdown-inner">
<button type="button" class="btn btn-default" id="privacy-button"><span id="privacy-icon" class="glyphicon glyphicon-lock" style="font-size:10px;"></span> <span id="privacy-text">Private project</span>  <span class="caret"></span> </button>
<ul class="dropdown-menu" id="privacy-menu">
<li><a href="#" id="private-menuitem"><span id="private-tick" class="glyphicon glyphicon-ok" style="font-size:10px;"> </span><span class="glyphicon glyphicon-lock" style="font-size:10px;"></span>  Private</a> </li>
<li><a href="#" id="public-menuitem"><span id="public-tick" class="glyphicon glyphicon-ok" style="font-size:10px; visibility:hidden;"> </span><span class="glyphicon glyphicon-globe" style="font-size:10px;"></span>  Public (read-only for others)</a> </li>
</ul>
</li>
</ul>
</li>
<li><a href="#contact">Contact</a> </li>
</ul>
</div>
</div>
<script>
$('.dropdown button').click(function(e) {
e.stopPropagation();
});
$('#privacy-button').click(function(e) {
e.stopPropagation();
$('.dropdown-inner').toggleClass('open').trigger('shown.bs.dropdown');
});
$('.dropdown').on('hide.bs.dropdown', function(e) { $('.dropdown-inner').removeClass('open');} );
$('#public-menuitem').click(function(e) {
e.stopPropagation();
$('#privacy-text').text('Public project');
$('#privacy-icon').toggleClass('glyphicon-lock').toggleClass('glyphicon-globe');
$('#public-tick').css('visibility','visible');
$('#private-tick').css('visibility','hidden');
});
$('#private-menuitem').click(function(e) {
e.stopPropagation();
$('#privacy-text').text('Private project');
$('#privacy-icon').toggleClass('glyphicon-lock').toggleClass('glyphicon-globe');
$('#public-tick').css('visibility','hidden');
$('#private-tick').css('visibility','visible');
});
</script>
</body>
</html>