Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get table column index using jQuery?
    primarykey
    data
    text
    <p>I have adequate knowledge about jQuery and its usage but today i got into a trouble of getting the column index of matching label inside <code>th</code> element of <code>table</code> using jQuery. I want to get the index of <code>th</code> element having <code>label</code> text as <strong>Mobile</strong>. The index should be 2 in this case. I am getting the actual index but this is not the correct way of doing it. So i want to know why jQuery is not giving me the right index using <code>index()</code> method.</p> <p>I have also written the <a href="http://jsfiddle.net/furqansafdar/DHkb4/2/" rel="nofollow">JS Fiddler</a> for this.</p> <p><strong>jQuery</strong></p> <pre class="lang-js prettyprint-override"><code>var elem = $('#tbl th'); var rIndex; alert('Length : ' + elem.length); var index = elem.filter( function(index){ var labelText = $(this).find('label').text(); //alert(index + ' - ' + labelText); var result = labelText == 'Mobile'; if (result) rIndex = index; return result; }).index(); alert("jQuery Index : " + index); alert("Actual Index : " + rIndex); </code></pre> <p><strong>HTML</strong></p> <pre class="lang-html prettyprint-override"><code>&lt;table id="tbl"&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;th&gt;&lt;label&gt;Application No.&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;/td&gt; &lt;th&gt;&lt;label&gt;Name&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;/td&gt; &lt;th&gt;&lt;label&gt;Mobile&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;/td&gt; &lt;th&gt;&lt;label&gt;Gender&lt;/label&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload