Search code examples
jqueryjquery-mobilejquery-mobile-listview

jquery select one item from li using class name


I have a ul>li structure using jQuery Mobile that looks like this:

<div>
<ul  data-role="listview">
  <li><a href="#" class="FoodDrinkItem">Hamburger<p class="FoodDrinkPrice">$9.00</p></a></li>
  <li> <a href="#" class="FoodDrinkItem">Club Sandwich<p class="FoodDrinkPrice">$7.00</p></a></li>
</ul>
</div>

I want to get the price of the item when I click the specific li.I would like to do this with classes because its easier to expand afterwards.

$(document).ready(function(){
    $(".FoodDrinkItem").click(function(){
        var price = $(".FoodDrinkPrice").text();
        $("#PopupFoodDrinkPrice").text(price);
    });
});

How can I take the className.text() of the selected li?


Solution

  • Working example: http://jsfiddle.net/Gajotres/VWkm9/

    $(document).on('click', 'ul li', function(){ 
        alert($(this).find('.FoodDrinkPrice').text());
    });     
    

    And you have 2 errors in your structure:

    This:

    <div data-role="listview">
    <ul>
    

    Should be this:

    <div>
    <ul data-role="listview">
    

    data-role="listview" must be placed inside an ul tag, without it listview will no be styled properly.

    And your p tag is not properly closed.