-1

I have a similar table like this:

 <table>
        <tr>
            <th class="theader"> .theader </th>
            <th class="theader"> .theader </th>
            <th class="theader"> .theader </th>
        </tr>
        <tr>
            <td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
            <td class="trow"> <input type='text' class='lat' name='long[]' /> </td>
            <td class="trow">
                <input type='text' class='place' name='place[]' />
                 <span class="check"></span> 
            </td>
        </tr>
        <tr>
            <td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
            <td class="trow"> <input type='text' class='lat' name='long[]' /> </td>
            <td class="trow">
                <input type='text' class='place' name='place[]' />
                 <span class="check"></span> 
            </td>
        </tr>
        <tr>
            <td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
            <td class="trow"> <input type='text' class='lat' name='long[]' /> </td>
            <td class="trow">
                <input type='text' class='place' name='place[]' />
                 <span class="check"></span> 
            </td>
        </tr>
    </table>

that actually looks like this picture: see the table enter image description here Now if I write a jQuery script to select any specific .check class (i.e. third row-third cell-span) and write any html message, what should be the selector? As you know if I use $('.check').html("Hello World"); then it prints Hello World in all the spans. I need to print the Hello World only in a specific span. TIA.

4 Answers4

0

You can use :eq(n) (selector) or .eq(n) (jquery method) (both 0-based) to target a specific, index-based element, eg:

$(".check:eq(2)").text("hello world")
$(".check").eq(2).text("hello world")

$(".check:eq(2)").text("hello world (2)")
$(".check").eq(1).text("hello world (1)")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <th class="theader"> .theader </th>
        <th class="theader"> .theader </th>
        <th class="theader"> .theader </th>
    </tr>
    <tr>
        <td class="troe"> .trow </td>
        <td class="trow"> .trow </td>
        <td class="trow"> <span class="check"></span> </td>
    </tr>
    <tr>
        <td class="troe"> .trow </td>
        <td class="trow"> .trow </td>
        <td class="trow"> <span class="check"></span> </td>
    </tr>
    <tr>
        <td class="troe"> .trow </td>
        <td class="trow"> .trow </td>
        <td class="trow"> <span class="check"></span> </td>
    </tr>
</table>
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • That's great, but i need to select **.check** class on `blur()` event for any input field of that row, in that case how can I get the selector? TIA – Sophia Jose Sep 07 '18 at 13:40
0

I suggest you use :eq()

To select the third row, third cell, span, you could use something like this

$( "table tr:eq(3) .check" ).text('CONTENT');

Here's a working fiddle


Regarding your comment, I updated the code in the fiddle to this

$('input').on('blur', function() {
    var t = $(this);
    t.closest('tr').find('.check').text('Hello World');
})

What this does is when you click outside the input field (blur event), it get's the row element and searches for the .check element. Instead of .text, you can do whatever you want with it once it's selected.

VVV
  • 7,563
  • 3
  • 34
  • 55
0

you can make use of nth-child and get third row third cell span. see below code

$(function(){
  $("table tr:nth-child(4) td:nth-child(3) span.check").html("Changed text");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <th class="theader"> .theader </th>
        <th class="theader"> .theader </th>
        <th class="theader"> .theader </th>
    </tr>
    <tr>
        <td class="troe"> .trow </th>
        <td class="trow"> .trow </th>
        <td class="trow"> <span class="check"></span> </th>
    </tr>
    <tr>
        <td class="troe"> .trow </th>
        <td class="trow"> .trow </th>
        <td class="trow"> <span class="check"></span> </th>
    </tr>
    <tr>
        <td class="troe"> .trow </th>
        <td class="trow"> .trow </th>
        <td class="trow"> <span class="check"></span> </th>
    </tr>
</table>
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
0

try using :nth-child(2)

$(function() {
  $("tr:nth-child(2)").find('.check').html('test');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th class="theader"> .theader </th>
    <th class="theader"> .theader </th>
    <th class="theader"> .theader </th>
  </tr>
  <tr>
    <td class="troe"> .trow </th>
      <td class="trow"> .trow </th>
        <td class="trow"> <span class="check"></span> </th>
  </tr>
  <tr>
    <td class="troe"> .trow </th>
      <td class="trow"> .trow </th>
        <td class="trow"> <span class="check"></span> </th>
  </tr>
  <tr>
    <td class="troe"> .trow </th>
      <td class="trow"> .trow </th>
        <td class="trow"> <span class="check"></span> </th>
  </tr>
</table>
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39