javascript - Error getting the index of a TR with jQuery -


i have problem index of table. index jquery index() method, returns 1. html table:

<table id="tabla_materia" cellspacing="0" cellpadding="0" style="width:500px; margin-top:50px;">     <tbody>         <tr>             <th colspan="6">                 lista de materia prima             </th>         </tr>         <tr>             <th class="pk"></th>             <th>codigo</th>             <th>codigo secundario</th>             <th>producto</th>             <th>cantidad requerida</th>             <th>cantidad segun formula</th>             <th>cantidad total</th>             <th>costo unitario</th>             <th>costo total</th>         </tr>         <tr class="ptype">             <td colspan="10"><strong>3.1.01.1 carnes - vacunas</strong>                 <input type="hidden" class="ptype" value="138">             </td>         </tr>         <tr class="item">             <td class="pk">1</td>             <td class="code" rowspan="1">                 <input type="hidden" value="300000563" class="id">                 <input type="hidden" value="31011029" class="code">31011029</td>             <td class="code2" rowspan="1">32146</td>             <td class="name" rowspan="1">sambari x kilo</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,816</span>                 <input type="hidden" value="0.8163265306122448" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="5.7142857142857135" class="tota_quantity"><span class="replaceme">5,714</span>             </td>             <td class="unitary_cost">17.000,00</td>             <td class="total_cost"><span class="replaceme">97.142,86</span>                 <input type="hidden" class="total_cost" value="97.142,86">             </td>         </tr>         <tr class="ptype">             <td colspan="10"><strong>3.1.03.1  despensa-aceites y grasas</strong>                 <input type="hidden" class="ptype" value="148">             </td>         </tr>         <tr class="item">             <td class="pk">2</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1775" class="id">                 <input type="hidden" value="31031005" class="code">31031005</td>             <td class="code2" rowspan="1">31101</td>             <td class="name" rowspan="1">aceite granel x lt</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,020</span>                 <input type="hidden" value="0.02040816326530612" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.14285714285714285" class="tota_quantity"><span class="replaceme">0,143</span>             </td>             <td class="unitary_cost">7.300,00</td>             <td class="total_cost"><span class="replaceme">1.042,86</span>                 <input type="hidden" class="total_cost" value="1.042,86">             </td>         </tr>         <tr class="ptype">             <td colspan="10"><strong>3.1.03.4 despensa - huevos </strong>                 <input type="hidden" class="ptype" value="151">             </td>         </tr>         <tr class="item">             <td class="pk">3</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1718" class="id">                 <input type="hidden" value="31034001" class="code">31034001</td>             <td class="code2" rowspan="1">31401</td>             <td class="name" rowspan="1">huevos x unidad</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">6,122</span>                 <input type="hidden" value="6.122448979591836" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="42.857142857142854" class="tota_quantity"><span class="replaceme">42,857</span>             </td>             <td class="unitary_cost">641,67</td>             <td class="total_cost"><span class="replaceme">27.500,00</span>                 <input type="hidden" class="total_cost" value="27.500,00">             </td>         </tr>         <tr class="ptype">             <td colspan="10"><strong>3.1.03.5 despensa -condimentos y caldos</strong>                 <input type="hidden" class="ptype" value="152">             </td>         </tr>         <tr class="item">             <td class="pk">4</td>             <td class="code" rowspan="1">                 <input type="hidden" value="300000777" class="id">                 <input type="hidden" value="31035014" class="code">31035014</td>             <td class="code2" rowspan="1">31517</td>             <td class="name" rowspan="1">comino x kg</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,003</span>                 <input type="hidden" value="0.0026122448979591837" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.018285714285714287" class="tota_quantity"><span class="replaceme">0,018</span>             </td>             <td class="unitary_cost">83.000,00</td>             <td class="total_cost"><span class="replaceme">1.517,71</span>                 <input type="hidden" class="total_cost" value="1.517,71">             </td>         </tr>         <tr class="item">             <td class="pk">5</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1873" class="id">                 <input type="hidden" value="31035040" class="code">31035040</td>             <td class="code2" rowspan="1">31503</td>             <td class="name" rowspan="1">pimienta negra en grano x kl</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,007</span>                 <input type="hidden" value="0.0065306122448979586" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.04571428571428571" class="tota_quantity"><span class="replaceme">0,046</span>             </td>             <td class="unitary_cost">69.500,00</td>             <td class="total_cost"><span class="replaceme">3.177,14</span>                 <input type="hidden" class="total_cost" value="3.177,14">             </td>         </tr>         <tr class="item">             <td class="pk">6</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1752" class="id">                 <input type="hidden" value="31035047" class="code">31035047</td>             <td class="code2" rowspan="1">31501</td>             <td class="name" rowspan="1">sal fina x kl</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,065</span>                 <input type="hidden" value="0.06530612244897958" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.4571428571428571" class="tota_quantity"><span class="replaceme">0,457</span>             </td>             <td class="unitary_cost">1.900,00</td>             <td class="total_cost"><span class="replaceme">868,57</span>                 <input type="hidden" class="total_cost" value="868,57">             </td>         </tr>         <tr class="item">             <td class="pk">7</td>             <td class="code" rowspan="1">                 <input type="hidden" value="2788" class="id">                 <input type="hidden" value="31035048" class="code">31035048</td>             <td class="code2" rowspan="1">31502</td>             <td class="name" rowspan="1">sal gruesa x kg</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,016</span>                 <input type="hidden" value="0.016326530612244896" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.11428571428571427" class="tota_quantity"><span class="replaceme">0,114</span>             </td>             <td class="unitary_cost">1.020,00</td>             <td class="total_cost"><span class="replaceme">116,57</span>                 <input type="hidden" class="total_cost" value="116,57">             </td>         </tr>         <tr class="ptype">             <td colspan="10"><strong>3.1.04.2 verduras</strong>                 <input type="hidden" class="ptype" value="158">             </td>         </tr>         <tr class="item">             <td class="pk">8</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1693" class="id">                 <input type="hidden" value="31042004" class="code">31042004</td>             <td class="code2" rowspan="1">33202</td>             <td class="name" rowspan="1">ajo x kl</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,031</span>                 <input type="hidden" value="0.03061224489795918" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.21428571428571425" class="tota_quantity"><span class="replaceme">0,214</span>             </td>             <td class="unitary_cost">16.000,00</td>             <td class="total_cost"><span class="replaceme">3.428,57</span>                 <input type="hidden" class="total_cost" value="3.428,57">             </td>         </tr>         <tr class="item">             <td class="pk">9</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1809" class="id">                 <input type="hidden" value="31042018" class="code">31042018</td>             <td class="code2" rowspan="1">mp0628</td>             <td class="name" rowspan="1">cebollita en hoja x kl</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,122</span>                 <input type="hidden" value="0.12244897959183672" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.857142857142857" class="tota_quantity"><span class="replaceme">0,857</span>             </td>             <td class="unitary_cost">5.423,73</td>             <td class="total_cost"><span class="replaceme">4.648,91</span>                 <input type="hidden" class="total_cost" value="4.648,91">             </td>         </tr>         <tr class="item">             <td class="pk">10</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1834" class="id">                 <input type="hidden" value="31042025" class="code">31042025</td>             <td class="code2" rowspan="1">33210</td>             <td class="name" rowspan="1">laurel x kl</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,001</span>                 <input type="hidden" value="0.0014285714285714286" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.01" class="tota_quantity"><span class="replaceme">0,010</span>             </td>             <td class="unitary_cost">30.000,30</td>             <td class="total_cost"><span class="replaceme">300,00</span>                 <input type="hidden" class="total_cost" value="300,00">             </td>         </tr>         <tr class="item">             <td class="pk">11</td>             <td class="code" rowspan="1">                 <input type="hidden" value="1783" class="id">                 <input type="hidden" value="31042064" class="code">31042064</td>             <td class="code2" rowspan="1">33237</td>             <td class="name" rowspan="1">apio x kilo</td>             <td class="requested_quantity"><span class="replaceme">7,000</span>                 <input type="hidden" value="7" class="requested_quantity">             </td>             <td class="formula_quantity"><span class="replaceme">0,027</span>                 <input type="hidden" value="0.026530612244897958" class="formula_quantity">             </td>             <td class="total_quantity">                 <input type="hidden" value="0.18571428571428572" class="tota_quantity"><span class="replaceme">0,186</span>             </td>             <td class="unitary_cost">4.761,90</td>             <td class="total_cost"><span class="replaceme">884,35</span>                 <input type="hidden" class="total_cost" value="884,35">             </td>         </tr>     </tbody> </table> 

this jquery code:

var indicemateria = $('table#tabla_materia tr.ptype input.ptype[value="148"]').index(); console.log('fila de la materia prima: '+indicemateria); // 1 

what problem??. please help. thank you.

the selector selects td element , returns index of td 1 since td element inside parent tr. use closest('tr') index of tr.

var indicemateria = $('#tabla_materia tr.ptype input.ptype[value="148"]').closest('tr').index(); //                                                                        ^^^^^^^^^^^^^ 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -