1

I have a html table with few columns and rows. in each row there is a button in a TD. when button will be clicked then i want to set some text into current or closest td of button.

This way i tried but nothing happening when button clicked.

$('#tblAppointments').on('click', '#btnbook', function () {

$(this).closest('td').html('Booked');

$(this).parent.html('Booked');

});

Table HTML

<table id="tblAppointments" class="table table-striped">
<thead class="thead-dark">
<tr>
    <th scope="col">Doctor Name</th>
    <th scope="col">Available Date</th>
    <th scope="col">Available Time</th>
    <th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td><input id="hdndocid" value="1" type="hidden">
<input id="hdnpatientemail" value="popy@gmail.com" type="hidden">Dr Debasis Saha</td>
<td>27/05/2018</td>
<td>10:10 A.M.</td>
<td><button id="btnbook" type="button" class="btn btn-info">Book Now</button></td>
</tbody>
</table>

This code is not working. I want to remove button from td and set some text like Booked. what is mistake in my code.

EDIT

See how i am doing full code

$('#tblAppointments').on('click', '.btnbook', function () {
    var rowid = $(this).closest('tr').find("input[id*='hdndocid']").val();
    var email = $(this).closest('tr').find("input[id*='hdnpatientemail']").val();
    //$(this).closest('td').html('Booked');

    var baseurl = '@ConfigurationManager.AppSettings["baseAddress"]' + 'api/Appointments/BookAppointment';
    //alert(baseurl);
    $.ajax({
        url: baseurl,
        type: 'POST',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({ email: email, rowid: rowid }),
        success: function (data, textStatus, xhr) {
            if(data=='SUCCESS');
            {
                $(this).closest('td').html('Booked');
                return false;

            }
        },
        error: function (xhr, textStatus, errorThrown) {
            var err = eval("(" + xhr.responseText + ")");
            alert('Error ' + err.Message);
            console.log(textStatus);
        }

    }).done(function () {

    });
});

if i write these line at top of button click then it is working

                    $(this).closest('td').html('Booked');
                    return false;

but when i put this line inside jquery ajax success then it is not working

 success: function (data, textStatus, xhr) {
                        if(data=='SUCCESS');
                        {
                            $(this).closest('td').html('Booked');
                            return false;

                        }
                    },

Please suggest what to do as a result code should work inside success.

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 2
    Do you have more than one `id="btnbook"`? IDs are required to be unique. Use a class instad. – Barmar May 26 '18 at 21:48
  • 1
    If that's not the problem, add your HTML to the question. Make it an executable [Stack Snippet](https://stackoverflow.blog/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – Barmar May 26 '18 at 21:50
  • html table added. i want when i will click button in td then button will be removed from td and a text will be added in that td. – Thomas May 26 '18 at 21:55
  • @Barmar i used class but still no luck. here is my code `$('#tblAppointments').on('click', '.btn-info', function () { $(this).closest('td').html('Booked'); $(this).parent.html('Booked'); });` – Thomas May 26 '18 at 22:02
  • 1
    Code works fine using class instead of id https://jsfiddle.net/xey0qz13/1/ – charlietfl May 26 '18 at 22:21
  • Note that `parent` is a jQuery method...not a property. Should see error in browser console trying `$(this).parent.html('Booked'); ` – charlietfl May 26 '18 at 22:21
  • @charlietfl i test your code it is working but the same is not working at my side. i am using version jquery-1.10.2.js it has any issue. you used different version. – Thomas May 26 '18 at 22:38
  • Version shouldn't matter so long as it's not really old and doesn't support `on()` which yours does. Any errors in console? Does table exist when you run that code? – charlietfl May 26 '18 at 22:44
  • @charlietfl please see my edit section where i describe the problem in more detail. – Thomas May 26 '18 at 22:53
  • 1
    Ahhh...because `this` inside success callback isn't what you think it is. See [How to access the correct `this` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – charlietfl May 26 '18 at 22:56
  • i tried this code this.closest('td').html('Booked'); but still not luck from jquery ajax success. so give me bit code which tell me what to change. – Thomas May 26 '18 at 22:58
  • Read that link. Store a reference to `this` outside of the `$.ajax` to use inside the callback – charlietfl May 26 '18 at 22:58
  • Or can also add option to $.ajax `context:this` – charlietfl May 26 '18 at 22:59
  • yes worked i store $(this) to self variable and later call like this from ajax success self.closest('td').html('Booked'); and it worked. thanks a lot for your time and guidance. – Thomas May 26 '18 at 23:00
  • @charlietfl i am eager to know why $(this) will not work in ajax success callback....please share the knowledge. thanks – Thomas May 26 '18 at 23:50

1 Answers1

0

If the table has more than one row, having more than one element with the same id is invalid html, and you should use a class selector instead:

<td>
    <button type="button" class="btn btn-info btnbook">Book Now</button>
</td>

<script>
    $(function(){
        $('#tblAppointments').on('click', '.btnbook', function () {
            $(this).closest('td').text('Booked');
        });   
    });
</script>

(and $(this).parent.html('Booked'); if you do use it, should be $(this).parent().html('Booked'); but the .closest('td') will select the closest 'td' up the chain in any case so it is not needed, based on your html).

If you've referenced jQuery correctly, and you do not have another element with the id = tblAppointments, this should work.

Based on your updated code, put this code ahead of $.ajax:

var _this = $(this);

and change the success function to be:

_this.closest('td').html('Booked');
RickL
  • 3,318
  • 10
  • 38
  • 39
  • same is not working at my side. i am using version jquery-1.10.2.js it has any issue. you used different version – Thomas May 26 '18 at 22:39
  • It should work if things are set up correctly - jQuery version should be ok - make sure the reference to the jQuery source file is in the section, and make sure you don't have any duplicate ids on the page. The only other issues could be if the table is in a modal (I'm assuming it's not ..?) - and make sure the script is wrapped in the document.ready function I gave in the answer. Double check these things and it should work. – RickL May 26 '18 at 22:43
  • please see my edit section where i describe the problem in more detail. – Thomas May 26 '18 at 22:53
  • I've updated the answer - your reference to $(this) should be defined before entering $.ajax – RickL May 26 '18 at 23:03
  • i am eager to know why $(this) will not work in ajax success callback....please share the knowledge. thanks – Thomas May 26 '18 at 23:50
  • 1
    Inside the success function (which is a callback), "this" refers to the jqXHR object of the Ajax call, not the element the event handler was bound to - see [this more complete answer](https://stackoverflow.com/a/6394826/5957979) for more info – RickL May 27 '18 at 00:00