JQuery getting ul based on matching in child text inside anchor inside with variable value

I have the following javascript variable:

<script>
   var theme="Videocameras";
</script>

And the following html code:

<div>    
     <span>Tecnology</span>
     <ul id="menu">
         <li>
            <a href="#">Multimedia</a>
            <ul>
                <li>
                    <a href="#">Videocameras</a>
                </li>

                <li>
                    <a href="#">Cameras</a>
                </li>

                <li>
                    <a href="#">MP3 and MP4</a>
                </li>                  
            </ul> 
        </li> 
            <li>
            <a href="#">Telephony</a>
            <ul>              
                <li>
                    <a href="#">Mobile Phones</a>
                </li>

            </ul> 
        </li> 
     </ul>
</div>

I would like to know how to get a unique ul that has an href inside with text that matches the value of the variable.

Could you give me a hand? Regards.

+3
source share
2 answers

You can use and :contains .closest()

try it

 $("a:contains(" + theme + ")").closest("ul");

See working demo

+2
source

@rahul , :contains - , . , - :

$("#menu li > a").filter(function () { 
    return $(this).text() == theme; 
}).closest("ul");

: http://jsfiddle.net/AndyE/zU7bU/1/ ( @rahul)

+1

Source: https://habr.com/ru/post/1776894/


All Articles