Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:09:18+00:00 2026-06-03T02:09:18+00:00

I have a tree of lists in Html such as this: <ul class=myTree> <li

  • 0

I have a tree of lists in Html such as this:

<ul class="myTree">
    <li class="treeItem">Category 1
        <ul>
            <li class="treeItem">Subcategory 1 1</li>
            <li class="treeItem">Subcategory 1 2
                <ul>
                    <li class="plus"><input type="text" value="Add a product"/></li>
                </ul>
            </li>
        </ul>
    </li>
    <li class="treeItem">Category 2
        <ul>
            <li class="treeItem">Subcategory 1 1</li>
            <li class="treeItem">Subcategory 1 2
                <ul>
                    <li class="plus"><input type="text" value="Add a product"/></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

So as you can see, it’s a kinda elaborated list.

Now what I need is a click event on those “treeItem” class elements that would retrieve the corresponding name (Category 1, Subcategory 1 1, etc).

I came up with this:

$(".myTree").on("click", ".treeItem", function() {
    var categoryName = $(this).text();
    alert(categoryName);
});

The only problem I’m facing right now is that first of all, categoryName contains

Category 2

                Subcategory 1 1
                Subcategory 1 2

when I click on a “level 1” <li> (such as Category 1). I could bypass this by adding some catname attribute to my <li>‘s but most importantly, when I click on a “level 2” <li> (such as Subcategory 1 1), it first triggers click event on level 2 <li>, does the stuff and then fires a new click event on level 1 <li>…

It’s quite obvious why it does that, but I need a way around to have it only happen once in the highest level (when I click on “Subcategory 1 1”, it should NOT fire an event on “Category 1”).

It’s also worth noting that this tree will be generated automatically, which means I could label my treeItem‘s with some other class such as “level1” or something like that but if I could avoid that, I’d love it.

… Alright this question might need a summary as I felt I kinda lost myself in it…

  1. I need a click event on a tree of lists
  2. I need that click event to only be fired at the highest level (the deepest).
  3. Adding some class names is possible but would like to be avoided.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:09:19+00:00Added an answer on June 3, 2026 at 2:09 am

    First of all you need to use event.stopPropagation to stop the event bubbling up the DOM and repeating itself. Then you need to get the first text node within the li. Try this:

    $(".myTree").on("click", ".treeItem", function(e) {
        e.stopPropagation();
        var categoryName = $(this).contents().filter(function() {
            return !!$.trim(this.innerHTML||this.data);
        }).first().text();
        alert(categoryName);
    });
    

    Example fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a deep tree structure built with lists, containing lists and floats. I
i have two lists: mylist1=[['a','1'],['a',''],['a','1'],['a','3'],['b','2'],['c','2'],['c','3'],['c',''],['c','2']] and mylist2=[['a','1','lemon'],['a','2','coconut'],['a','3','chocolate'],['a','4','watermelon'],['b','1','mango'],['b','2','apple'],['b','3','tree'],['b','4','apple'],['c','1','water'],['c','2','fire'],['c','3','mountain']] I want to find out those lists
I have a binary tree in unordered list that looks like this: <ul> <li>1
I have a tree walker like this: function: ^(FUNCTION_TOK fcname=IDENTIFIER param=functionParameters*){ a_param_arrayList.add(param); } ;
I have an html that has a link: <a id=myLink href=/aa.html title=New Bangoo class=bangoo>Bangoo</a>
I have following HTML <div id=finalTree> <ul> <li class=last style=display: list-item;> <a id=DataSheets href=#>Data
I have the HTML code: <span style=padding-right:100px; class=tree_list> <p>Hover Here</p> <span class=admin_tools id=tree_list_tool> <a
I have been trying to parse with etree.HTML() a text encoded as UTF-8 without
I have a tree-like object, managed by Hibernate. The object has a list of
I have a xsl:variable ($features-list) specified in my XSL which contains a tree fragment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.