Reputation: 493
I am working on MVC 5 project, there I have to show this, Electrical > Lighting > HT<.. to idSearch-item
div with respect to each click.
Example: If the user click on Lighting, then show like Electrical > Lighting or even if click on HT< then show like Electrical > Lighting > HT< like that.
Here is html,
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Electrical<input type="hidden" value="28"></li>
<li>
<a href="#">Lighting</a>
<input type="hidden" value="29">
</li>
<li>
<a href="#">HT & LT</a>
<input type="hidden" value="30">
</li>
</ul>
</li>
I have tried below script code,
$("li a").each(function () {
$(this).click(function () {
document.getElementById("idSearch-item").innerHTML = '<ul class="breadcrumb"><li><a href="#">' + $(this).text() + '</a></li><li>' + $(this).text() + '</li></ul>';
$('#idSearch-item').show();
});
});
<div class="row" style="display:none;padding:0 0 20px 0;float: left;" id="idSearch-item">
</div>
But now showing like this,
But I want see like this,
How can I do this?
Upvotes: 1
Views: 541
Reputation: 13997
You can use jquery's prevAll()
:
$("li a").click(function () {
var breadcrumbs = [$(this).text()];
var previous = $(this).parent().prevAll("li");
previous.each(function(el) {
breadcrumbs.push($(el).find("a").text());
});
var breadcrumbHtml = $("<ul class='breadcrumb'></ul>");
breadcrumbs.reverse().each(function(item) {
breadcrumbHtml.append("<li><a href='#'>" + item + "</a></li>");
});
$("#idSearch-item").html(breadcrumbHtml);
$('#idSearch-item').show();
});
Note: written from the top of my head, not tested
Upvotes: 1
Reputation: 3348
You can use jQuery's prevAll to get all previous li's. Here is a working jsfiddle: https://jsfiddle.net/t0cjxrns/1/
HTML
<div id="idSearch-item"></div>
<li class="col-sm-3">
<ul>
<li class="dropdown-header">Electrical<input type="hidden" value="28"></li>
<li>
<a href="#">Lighting</a>
<input type="hidden" value="29">
</li>
<li>
<a href="#">HT & LT</a>
<input type="hidden" value="30">
</li>
</ul>
</li>
Javascript
$("li a").each(function () {
$(this).click(function () {
var $this = $(this);
//build breadcrumb html
var $breadcrumb = $('<ul class="breadcrumb">');
//add all previous li items to the selection
$this.add($this.parent("li").prevAll("li")).each(function() {
//iterate each item in the selection and build an li for the breadcrumb
$breadcrumb.append('<li><a href="#">' + $(this).text() + '</a></li>');
});
var $idSearchItem = $("#idSearch-item");
//set the html of the idSearch-item (jQuery way) and show it
$idSearchItem.html($breadcrumb);
$idSearchItem.show();
});
});
Upvotes: 1
Reputation: 27041
Get the index of the current clicked element
$(this).parent().index()
We want to get the li
index, so since we clicked on a
we need to use .parent()
to select our li
Then add all elements before that to your breadcrumbs
$("li a").click(function() {
var bc = '<ul class="breadcrumb">';
var index = $(this).parent().index();
$(this).closest("ul").find("li").each(function(i, x) {
if (i < index) {
bc += '<li><a href="#">' + $(x).text() + '</a></li>';
}
})
bc += '<li>' + $(this).text() + '</li></ul>';
$('#idSearch-item').html(bc).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" style="display:none;padding:0 0 20px 0;float: left;" id="idSearch-item">
</div>
<br><br>
<br><br>
<br><br>
<br><br>
<ul>
<li class="dropdown-header">Electrical<input type="hidden" value="28"></li>
<li>
<a href="#">Lighting</a>
<input type="hidden" value="29">
</li>
<li>
<a href="#">HT & LT</a>
<input type="hidden" value="30">
</li>
</ul>
Upvotes: 1