0

This is the unordered list:

<ul id="all-categories" class="categories-list nav nav-tabs text-center">
 <li class="category" >
     <a class="dropdown-toggle" data-toggle="dropdown" href="#">Liquor</a>
         <ul id="liquor-category" class="dropdown-menu">
            <li><a href="#">Bourbon</a></li>
            <li><a href="#">Gin</a></li>
            <li><a href="#">Rum</a></li>
            <li><a href="#">Tequila</a></li> 
            <li><a href="#">Vodka</a></li> 
            <li><a href="#">Whiskey</a></li>  
          </ul>


  </li>
   ...
</ul

And this is the code I'm using to get the text and use it as a label elsewhere on the page:

var liquor_selected  = document.querySelector('#liquor-category');
      var selected = liquor_selected.addEventListener('click',function(e){
          document.getElementById("category-name-box").innerHTML = "Liquor Sub-Category Here";
      });

It prints the generic text, but I'd like it to print the sub-category. A little help please.

2
  • What do you mean with sub-category? Commented Mar 18, 2016 at 3:39
  • sorry, I forgot some of the code in the above example so have updated the example code. There is a nav bar with types of alcohol. One being "Liquor", The liquor category displays as a submenu when "Liquor" is clicked. I'm looking to grab the text of the selected subcategory (for ex: "Whiskey"). Commented Mar 19, 2016 at 17:00

2 Answers 2

3

The click handler is bound to the parent so you have to find the e.target clicked:

var liquor_selected = document.querySelector('#liquor-category');
var selected = liquor_selected.addEventListener('click', function (e) {
  document.getElementById("category-name-box").innerHTML = e.target.innerText;
});

In jQuery you can do the same:

$('#liquor-category').on('click', function (e) {
  $('#category-name-box').text($(e.target).text());
});

or you can bind to the a selector more easily in jQuery

$('#liquor-category a').on('click', function (e) {
  $('#category-name-box').text($(this).text());
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Tom! The first answer did the trick. I haven't tried the others, but I am curious, is there any benefit to using the other methods you listed? What are the differences in the 3 approaches?
jQuery stuff is a little more read-able. If it's on the page use it! Other than that having efficient event handlers is a little of an advanced topic that is explained pretty well in this video: bocoup.com/screencasts/more-efficient-event-handlers
0

I'm assuming that you want to achieve the following (in jQuery)

Example in JsFiddle: https://jsfiddle.net/282u8avw/

HTML:

          <ul id="liquor-category" class="dropdown-menu">
            <li><a href="#">Bourbon</a></li>
            <li><a href="#">Gin</a></li>
            <li><a href="#">Rum</a></li>
            <li><a href="#">Tequila</a></li> 
            <li><a href="#">Vodka</a></li> 
            <li><a href="#">Whiskey</a></li>  
          </ul>
          <div id="category-name-box">

          </div>

Javascript/jQuery:

  $(document).ready(function() {
    $('#liquor-category a').click(function() {
      $('#category-name-box').text($(this).text());
    });
  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.