JavaScript Grabbing parent text using .text()

I am having trouble getting the text 'Vegetable' from the DOM

   <div class="input-group">
    <ul>
        <li>
            <span class="input-checkbox">
                <input type="checkbox" aria-label="...">
            </span>
            Vegetable
        </li>
     </ul>
    </div>

Currently, I have an event handler for when I check the box to grab the text of the element of the list, but what I am doing is not working: $(e.target).parent().text(). My thinking was that I am targeting "input-checkbox", so i need to get the parent and then grab the text of it. Is that the right approach or is it something wrong with my jQuery?

Events Code:

Template.produceList.events({
    'click .input-checkbox': function(e) {
        e.preventDefault();
        console.log('clicked on filter');
        var text = $(e.target).parent().text();
        return;
    }
});
Answer:1

You need to go two levels up because "input" is inside "span" which is inside "li", and the text "Vegetable" is inside "li".

$(e.target).parent().parent().text()

I have created an example here (it's a little modified form of your code): https://jsfiddle.net/x5yb2m30/

Another way to do this would be to use parents() and look for li tag:

$(e.target).parents("li").text()
Answer:2

I have a timer in a template, which updates every second using angular's $interval service. var now = moment(); var startActual = moment(scope.vm.start_actual); var duration = ...

I have a timer in a template, which updates every second using angular's $interval service. var now = moment(); var startActual = moment(scope.vm.start_actual); var duration = ...

  1. using angular material
  2. using angular cli
  3. using angular in visual studio
  4. using angularjs
  5. using angular with django
  6. using angular and react together
  7. using angular material icons
  8. using angular with asp.net mvc
  9. using angular with java
  10. using angular material with bootstrap
  11. using angular with flask
  12. using angular with express
  13. using angular elements
  14. using angular in visual studio 2019
  15. using angular router
  16. using angular with electron
  17. using angular with react
  18. using angular in visual studio code
  19. using angular with .net
  20. using angular directives

I have a simple set of statements that actually work (I have run that code and it does what I expect of it) that looks as follow: result_xml = result.jqxhr.responseXML; a = jQuery("data[name='...

I have a simple set of statements that actually work (I have run that code and it does what I expect of it) that looks as follow: result_xml = result.jqxhr.responseXML; a = jQuery("data[name='...

  1. dealing with scrollbar jquery

I am using WebGL to do hardware skinning, however updating my model node hierarchies is causing a huge hit for performance. Every node needs to query its current location/rotation/scale keyframes, ...

I am using WebGL to do hardware skinning, however updating my model node hierarchies is causing a huge hit for performance. Every node needs to query its current location/rotation/scale keyframes, ...

I have a small issue with conditional rendering of some part of html in jade, doing my best not to duplicate code. I have the following jade bit if(loggedIn && !search) form(action="/list/...

I have a small issue with conditional rendering of some part of html in jade, doing my best not to duplicate code. I have the following jade bit if(loggedIn && !search) form(action="/list/...