简体   繁体   中英

Get text inside a anchor list element using jquery library

 <ul class="dropdown-menu" id="neither_nor_selector"  style="text-decoration: none;left: 18%;min-width: 65px;">
          <li><a title="neither">neither</a></li>
          <li><a title="nor">nor</a></li>
 </ul>

How to display the selected list item text

$("#neither_nor_selector option:selected").text()?

You could do something like this if you did not want to use a select.

   $(".dropdown-menu li").on("click", function(){
       //get the text here
       var myText = $(this).find("a").text();
       console.log(myText);
   });

This will just hook onto clicking any of the li's (i'm not sure what event your using to select so it could change i guess). and then you can do with it what you need.

Alternatively you could just re-style the select tag to look how you please (assuming you want a real dropdown here).

Example fiddle - https://jsfiddle.net/v53aubL3/

You can assign a class to anything and then give each element a different id:

 <li class="foo" id="first"><a title="neither">neither</a></li>
              <li class="foo" id="nor"><a title="nor">nor</a></li>

js:

$(".foo").click(function() {

var bar = $(this).attr('id');
alert(bar);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM