I want to switch between the bootstrap theme on click of link of that theme name here is the html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=1.0, user-scalable=no">
<title>Theme Changer</title>
<meta name="description" content="" />
<meta name="keywords" content="">
<link id="theme" rel="stylesheet" href="css/bootstrap.css" />
</head>
<body>
<nav class="navbar navbar-expand-sm bg-light navbar-light">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</nav>
<div class="row">
<div class="col-md-6">
<a id="Cyborg" href="" class="btn btn-block">Cyborg</a>
</div>
<div class="col-md-6">
<a id="Lux" href="" class="btn btn-block">Lux</a>
</div>
</div>
<footer>
<p>Theme Changer</p>
</footer>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function () {
$('#Cyborg').click(function () {
$('#theme').attr('href', 'css/bootstrapcyborg.css');
});
$('#Lux').click(function () {
$('#theme').attr('href', 'css/bootstraplux.css');
});
});
</script>
</body>
</html>
I have created link for initial page load css and then on click of either of the link i am replacing
original href attribute with i have assign in jquery.
Can anybody suggest where i am going wrong.
I can think of few solutions for that, but I would recommend first one.
atags usebuttonwithouthrefattribute.href="javascript:void(0)"orhref="#"to youratagsOR
But keep in mind that having
'#'ashrefwill take you back to the top of the page whenaclicked.And also following https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a it's bad practice
atags your way, because browser will remove anchor stylingExplanation: Because
#Cyborgor#Luxare anchor elements their default browser behaviour when clicked is to initiates navigation to its URL, because you have empty string insidehrefbrowser simply reloads the page. Then browser goes back to the state after first load and you see no effect.Hope I understand you correctly and this will help