javascript - How to select all list items but list items don't have nested child of <ul> or <ol> -
i want select list items in child of ".favdelparentfold" make sure list items not have nested child of <ul>
. if list items have nested child "<ul>
" don't want trigger it.
html
<li class="favdelparentfold"> <a href=""><span class="caret-right"></span></a> <span class="folder"></span><span>fold2</span><span class="trashcan">press me</span> <ul> <li><a href="">one 1</a><span class="trashcan">press me</span></li> <li><a href="">two 2</a><span class="trashcan">press me</span></li> <li><a href="">three 3</a><span class="trashcan">press me</span></li> <li> <a href="">four 4</a><span class="trashcan">press me</span> <ul> <li>four 1.1</li> <li>four 1.2</li> </ul> </li> </ul> </li>
js file:
$(document).ready(function () { $(".favdelparentfold ~ ul li.trashcan").click(function () { var tr = $(this).prev('span').text(); var pa = $(this).closet().text(); alert(tr); alert(pa); }); });
ex:
if click on 1 1, 2 2, 3 three can value , parent value 4 four have child of <ul>
want avoid triggers.
expected o/p:
if click "one 1" need 1 one , fold2
if click "one 2" need 1 2 , fold2
if click "one 3" need 1 3 , fold2
but don't trigger "four 4"
i think looking following.
$(".favdelparentfold ul li .ecm-trashcan").click(function() { var tr = $(this).prev().text(); var ma = $(this).closest('.favdelsubfold').find('#sfdel').text(); var pa = $(this).closest('.favdelparentfold').find('#pfdel').text(); alert(tr); if(tr!=ma) alert(ma); alert(pa); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li class="favdelparentfold"> <a href=""><span class="caret-right"></span></a> <span class="folder"></span><span>fold2</span><span class="trashcan">press me</span> <ul> <li><a href="">one 1</a><span class="trashcan">press me</span></li> <li><a href="">two 2</a><span class="trashcan">press me</span></li> <li><a href="">three 3</a><span class="trashcan">press me</span></li> <li> <a href="">four 4</a><span class="trashcan">press me</span> <ul> <li>four 1.1</li> <li>four 1.2</li> </ul> </li> </ul> </li>
update: according fiddle html js
this:
$(".favdelparentfold ul li .ecm-trashcan").click(function () { if ($(this).parent().find('ul').length || $(this).closest('.favdelsubfold').length) return; var tr = $(this).prev('a').text(); var pa = $(this).closest('.favdelparentfold').find('#pfdel').text(); alert(tr); alert(pa); });
Comments
Post a Comment