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?
$(". RestauranstSection li"). click(function (){ alert($(this). text()); });
If you have HTML inside the LI elements and you only want to get the text, you need innerText or textContent .
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.
To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.
$(".RestauranstSection").on('click','li',function (){ alert($(this).text()); });
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With