Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sasafister's avatar

[jQuery] How to update id on which i clicked after AJAX success?

Everything works fine, except when I click "thums up" button, it always update my first row in table.

My code is on github, so you can see it there, and here is just excerpt

https://github.com/sasafister/scheduler/blob/materialdesign/resources/views/titles/index.blade.php

 var url = "{{ url("/") }}" + "/{{ Auth::user()->id }}" + "/titles"

    $("#upVote i").on("click", function() {
        $.ajax({
            type: 'POST',
            url: url + "/upvote" + "/" + this.id,
            success: function(data) {
                $('#numVote').html(data);
            }
        });
    });

Here is DOM

 @foreach ($titles as $title)
                @if($title->created_at > \Carbon\Carbon::today())
                    <tr id="votes">
                        <td>{{ \Carbon\Carbon::parse($title->time)->format('d.m.') }}</td>
                        {{--<td id="id">{{ $title->id}}</td>--}}
                        <td>{{ $title->user->name }}</td>
                        <td>{{ $title->title }}</td>
                        <td id="downVote"><i id="{{ $title->id }}" class="tiny material-icons red-text">thumb_down</i></td>
                        <td id="numVote">{{ $title->votes }}</td>
                        <td id="upVote"><i id="{{ $title->id }}" class="tiny material-icons green-text">thumb_up</i></td>
                        <td>{!! link_to_action('TitlesController@show', 'Edit', [$customer, $title->id]) !!}</td>
                    </tr>
                @elseif($title->created_at < \Carbon\Carbon::today())
                    <tr class="grey-text">
                        <td>{{ \Carbon\Carbon::parse($title->time)->format('d.m.') }}</td>
                        <td>{{ $title->user->name }}</td>
                        <td>{{ $title->title }}</td>
                        <td></td>
                    </tr>

                @endif
            @endforeach
0 likes
11 replies
bobbybouwmann's avatar

I think you need to use $(this).attr('id') here to get the correct ID.

Als you shouldn't be triggering the ID of the parent element. If you have multiple they will all have the same ID and that will not work, instead make it a class

<td class="upVote"><i id="{{ $title->id }}" class="tiny material-icons green-text">thumb_up</i></td>

$(".upVote i").on("click", function() {
    var id = this.attr('id');

    // Do your ajax stuff
});
sasafister's avatar

@bobbybouwmann thnx for reply, acutally, I got dyamic ID, everything works fine, but when I update data in #numVote (ajax success) then everthing is updated (I changed #numVote into .numVote)

d3xt3r's avatar

@sasafister Thing about #(id) and .(class)

#id => should be unique (only one per DOM) What you are trying to achieve could be done by

<tr id="votes"> < !-- even this is not correct way -->
                        <td>{{ \Carbon\Carbon::parse($title->time)->format('d.m.') }}</td>
                       ....
                        <td class="numVote">{{ $title->votes }}</td>
                        <td class="upVote"><i id="{{ $title->id }}" class="tiny material-icons green-text">thumb_up</i></td>
                        <td>{!! link_to_action('TitlesController@show', 'Edit', [$customer, $title->id]) !!}</td>
                    </tr>
 $(".upVote i").on("click", function() {
    $this = $(this);
        $.ajax({
            type: 'POST',
            url: url + "/upvote" + "/" + this.id,
            success: function(data) {
               $this.parent().parent().find('.numVote').html(data);
            }
        });
    });
arif's avatar

$("#upVote i").on("click", function() { $.ajax({ type: 'POST', url: url + "/upvote" + "/" + this.id, success: function(data) { $('#numVote').html(data); } }); });

it should be

$("table #upVote i").on("click", function() { var id = $(this).closest('i').attr('id'); $.ajax({ type: 'POST', url: url + "/upvote" + "/" + id, success: function(data) { $('#numVote').html(data); } }); });

sasafister's avatar

Nope @arif still the same thing. So the problem is only in updateing right row, response is good, id's are good, everyting is good if I refresh, so I need to solve updating

sasafister's avatar

Okay. So, everything works fine, that means, success method is doing his job, and in database everthing is updated as supposed to be. Only thing is that I need to updated correct (clicked) row in table.

Try this one:

user: sasa@zimo.co pass: 123456

http://dev.ozim.info/zimo_schedule_test/

arif's avatar

@sasafister i got it... u jst need to check id..

  1. put it it a tag means a id="{{ $title->id }}" class="upVote">thumb_down
  2. $("table tr td .upVote").on("click", function()
  3. var id = $(this).closest('i').attr('id'); 4.function(data) { $('#numVote').html(data); } }) instead of this use function(data) { $('#'+id).html(data); } })

use console.log to check your id and data

d3xt3r's avatar

@sasafister Did you try what was proposed, with respect to class and id(s). If you want to stick to id(s)

<tr id="votes-{{$title->id}}"> < !-- even this is not correct way -->
                        <td>{{ \Carbon\Carbon::parse($title->time)->format('d.m.') }}</td>
                       ....
                        <td id="numVote-{{$title->id}}">{{ $title->votes }}</td>
                        <td class="upVote"><i id="{{ $title->id }}" class="tiny material-icons green-text">thumb_up</i></td>
                        <td>{!! link_to_action('TitlesController@show', 'Edit', [$customer, $title->id]) !!}</td>
                    </tr>   

// and then
 $(".upVote i").on("click", function() {
    $this = $(this);
        $.ajax({
            type: 'POST',
            url: url + "/upvote" + "/" + this.id,
            success: function(data) {
               $("#numVote-" + $this.id).html(data);
            }
        });
    });

sasafister's avatar
sasafister
OP
Best Answer
Level 10

I got it. Your code @arif was all I needed. I put

$('#numVote' + $id).html(data);

and i put

 <td id="numVote{{ $title->id }}" >{{ $title->votes }}</td>

1 like

Please or to participate in this conversation.