0

Is it possible to call an html class with specific id, because for now I have same functionality in my class then I need to pull it through class + id to be specific.

I have button that populates data to my table:

<button type="button" onclick="loadData()">Get Data</button>

and I have two tables with the same class name:

<table class="sortable" id="1">
    <thead>
        <tr>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>    
        <tr>
            <td></td>
            <td></td>
        </tr>

    </tbody>
</table>


  <table class="sortable" id="2">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>    
        <tr>
            <td></td>
            <td></td>
        </tr>

    </tbody>
</table>

My aim is to update only the first table which has the id of 1. because right now when I clicked the button both of them will update since they have the same class name. Is it possible if they can Identify by class name + id?

here is my update:

 function loadData() {        

 $.ajax({
        url: '/Home/ReadDB',
        type: 'GET',

    dataType: 'json',
    success: function (data) {
        var row = '';
        $.each(data, function (i, item) {
            row += '<tr><td>' + item.LName + '</td><td>' + item.age
                + '</td><td>' + '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Add </button >' + '</td></tr>';

        });
        console.log(row)
        $('.sortable tbody').html(row); // --This one overrides my previous result and want to identify by id            
    },
    error: function (jqXhr, textStatus, errorThrown) {
        alert(errorThrown);
    }
});

}
Rock n' Roll
  • 924
  • 1
  • 12
  • 31

3 Answers3

0

Simple:

var firstTable = $('.sortable[id="1"]');
console.log(firstTable);
RyanB
  • 1,237
  • 1
  • 9
  • 27
  • Why not just firstTable = $('.sortable#1'); as your solution looks for an id that -begins- with '1' rather than one which -is- '1'. – rrd Nov 05 '18 at 08:19
  • Ah! must not have `^` in the expression. However, my solution can be used with any attribute of the element, not just `id` field. – RyanB Nov 05 '18 at 08:27
0

First change your ID name please read this answer https://stackoverflow.com/a/70586/2724173

and then concatenate them with like this

$('.sortable#IDname tbody').html(row);
Ismail Farooq
  • 4,949
  • 1
  • 20
  • 42
0

Try this

$.('#1.sortable tbody').html(row);
Jithin
  • 2,533
  • 1
  • 17
  • 41