Reputation: 1239
I want to find li id of current html.
jQuery
var CurrentHTML = document.location.pathname.match(/[^\/]+$/)[0];
In this i will get the current html name like Client.html or Vendor.html
.
After getting current html name, i want to get the parent li id of current html name like li1 or li2
.
Example
If my CurrentHTML is Candidate.html then i want to get parent li id (li4).
HTML
<ul class="nav">
<li id="li1"><a href="Client.html">Client</a></li>
<li id="li2"><a href="Vendor.html">Vendor</a></li>
<li id="li3"><a href="#">Recruitment</a>
<ul>
<li id="li4"><a href="Candidate.html">Candidate</a></li>
<li id="li5"><a href="Resource.html">Resource</a></li>
<li id="li6"><a href="ClientRequest.html">Client Request</a></li>
<li id="li7"><a href="CandidateAssignment.html">Candidate Assignment</a></li>
</ul>
</li>
<li id="li8"><a href="Project.html">Project</a></li>
<li id="li9"><a href="#">Security</a>
<ul>
<li id="li10"><a href="Roles.html">Roles</a></li>
<li id="li11"><a href="Users.html">Users</a></li>
</ul>
</li>
<li id="li12"><a href="#">Time Sheet</a>
<ul>
<li id="li13"><a href="TimeTracking.html">Tracking</a></li>
<li id="li14"><a href="TimeTrackingReport.html">Report</a></li>
<li id="li15"><a href="TimeSheetReport.html">Download</a></li>
</ul>
</li>
</ul>
Upvotes: 0
Views: 921
Reputation: 8161
Try to get element with href
attribute and then use .parent()
.
var CurrentHTML = document.location.pathname.match(/[^\/]+$/)[0],
liId = $('ul.nav li a[href="' + CurrentHTML + '"]').parent().attr('id');
Upvotes: 1
Reputation: 74738
You can get it like this:
var CurrentHTML = document.location.pathname.match(/[^\/]+$/)[0];
var liId = $('[href*="'+CurrentHTML+'"]').closest('li')[0].id;
$('[href*="'+CurrentHTML+'"]')
this gets your the <a>
anchor tag which contains the currentHTML
page then you can traverse up to its parent li with .closest('li')[0]
then get the id with .id
.
Upvotes: 1
Reputation: 8346
Use attribute selector
$('a[href="' + CurrentHTML + '"]').parent().attr('id');
Upvotes: 1
Reputation: 38102
You can do like this:
var CurrentHTML = document.location.pathname.match(/[^\/]+$/)[0],
liId = $('ul.nav li a[href="' + CurrentHTML + '"]').closest('li').attr('id');
Upvotes: 2