Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get the text inside <li>

Tags:

I have this div

<div class="RestauranstSection">     <label>         Restaurant:     </label>     <input type="text"/>     <input type="button" value="Search"/>     <ul>         <?php while ($row = $restaurants->fetch()) {             ?>             <li id="<?php echo $row['ID']; ?>">                 <?php echo $row['name']; ?>             </li>         <?php } ?>     </ul> </div> 

I want when press on any li elements to alert its text,

$(".RestauranstSection").on('click','li',function (){}); 

how please?

like image 986
Marco Dinatsoli Avatar asked Mar 18 '13 15:03

Marco Dinatsoli


People also ask

How can get Li tag text in jQuery?

$(". RestauranstSection li"). click(function (){ alert($(this). text()); });

How do I get text from Li?

If you have HTML inside the LI elements and you only want to get the text, you need innerText or textContent .

How do I get text inside a div using jQuery?

To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.

How can get input tag value in jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.


2 Answers

$(".RestauranstSection").on('click','li',function (){     alert($(this).text()); }); 
like image 164
jtepe Avatar answered Dec 10 '22 22:12

jtepe


You just have to select the li and register for the click event like so

$(".RestauranstSection li").click(function (){     alert($(this).text()); }); 

(you also might want to spell check RestaurantsSection ;)

like image 31
Michael Brown Avatar answered Dec 10 '22 21:12

Michael Brown