find the value of the table cell



  • Table. https://jsfiddle.net/kley/d81n04oo/

    <div class="table-responsive">
                <table class="table table-condensed table-hover table-bordered">
                    <thead>
                        <tr>
                            <th></th>
                            <th>Номенклатура</th>
                            <th>Ед.изм</th>
                            <th>Мин</th>
                            <th>Средн</th>
                            <th>Макс</th>
                        </tr>
                    </thead>
                    <tbody id="t_price">
              <tr>
                <td>
                <button type="button" class="add_item btn btn-primary btn-sm">+</button></td>
                <td>Кофе1</td>
                <td>кг</td>
                <td>300</td>
                <td>400</td>
                <td>500</td>
              </tr>
              <tr>
                <td><button type="button" class="add_item btn btn-primary btn-sm">+</button></td>
                <td>Кофе2</td>
                <td>уп</td>
                <td>350</td>
                <td>470</td>
                <td>600</td> 
               </tr>
               <tr>
                 <td><button type="button" class="add_item btn btn-primary btn-sm">+</button></td>
                 <td>Кофе3</td>
                 <td>г</td>
                 <td>110</td>
                 <td>200</td>
                 <td>350</td> 
               </tr>
             </tbody>
             </table>
             </div>
                </table>
            </div>
    

    In the first cell of the "+" button, you'll need to get the text of the "Nomenclair" column in this line.

    js:

    $('#t_price').on('click','.add_item',function(){
            var it=$(this).next('td').html();
                console.log(it);
            });
    

    The result is something undefined. What's the catch?



  • The catch is the button doesn't have a button. Next component, therefore $(this).next('td') He'll return the empty selter.

    You need to use, for example, https://api.jquery.com/parent/

    var it=$(this).parent().next('td').html();
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2