0

I am trying to call a jQuery function upon clicking on the 4th 'td' as below. But it is not calling the function, I have tried different ways to get the row from the table.

<table class='logtable'>
        <thead>
            <tr>
                <th>Time</th>
                <th>Name</th>
                <th>Log Note</th>
            </tr>
        </thead>
        <tbody>
            <?foreach ( $logRows as $logRow ){?>
            <tr class="logtr">
                <td><?=readable_date($logRow['date_created'])?></td>
                <td><?=$logRow['first_name']></td>
                <td><?=$logRow['log_note']?></td>
                <td class="js-delete-log"  data-log-id=<?=$logRow['log_id']?>><span class="delete"> <i class="fa fa-times"></i></span></td>

            </tr>
            <?}?>
        </tbody>
    </table>

Below is the JQuery function which is called from the 'td'.

// jQuery function to delete the log

$( "body" ).on( "click", ".js-delete-log", function(event) {
        var el = $(this);
        var log_id = el.data('log-id');

              $.post( "../ajax/delete_logs.php", {log_id:log_id,action:'delete_log'} )                         
              location.reload();
    });

});

Can anyone please help on what's the issue here?

I tried the below method also.

//   $("table.logtable tr.logtr td.js-delete-log").click(function() {  
5
  • 1
    any error in console Commented Dec 11, 2015 at 6:39
  • have you checked your code "<td><?=$logRow['first_name']></td>" you have missed the "?>" Commented Dec 11, 2015 at 6:44
  • just do a simple form if you are reloading the page no need for ajax Commented Dec 11, 2015 at 6:44
  • can you please check using alert that you have data-log-id in valid form that you want or not? Commented Dec 11, 2015 at 6:46
  • It seems that there is extra identation }); . Commented Dec 11, 2015 at 6:48

7 Answers 7

1

i think the problem come from this

 <td class="js-delete-log"  data-log-id=<?=$logRow['log_id']?>>

Try this one

<td class="js-delete-log"  data-log-id=<?php echo $logRow['log_id'] ?>>
Sign up to request clarification or add additional context in comments.

1 Comment

That's correct, but we can't say he is wrong here until we know his version.
0

Try using attr() , reload the page when ajax is successful:

var log_id = el.attr('data-log-id');
$.post( "../ajax/delete_logs.php", {log_id:log_id,action:'delete_log'},function(data){
location.reload();
});

1 Comment

why to use attr if jquery provide .data()
0

your code working fine you have extra "});" tag in your code may cause of problem

$( "body" ).on( "click", ".js-delete-log", function(event) {
        var el = $(this);
        var log_id = el.data('log-id');
        alert(log_id)

//              $.post( "../ajax/delete_logs.php", {log_id:log_id,action:'delete_log'} )                         
  //            location.reload();
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<table class='logtable'>
        <thead>
            <tr>
                <th>Time</th>
                <th>Name</th>
                <th>Log Note</th>
            </tr>
        </thead>
        <tbody>
            
            <tr class="logtr">
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td class="js-delete-log" data-log-id='50' >  4</td>

            </tr>
          
        </tbody>
    </table>

Comments

0

Can you try this? (just to ensure that it reaches this event handler so that we can confirm if the error is in event binding or later)

$( document.body ).on( "click", ".js-delete-log", function(event) {
        alert("clicked");
});

1 Comment

@Wings2fly if this is not working then the issue in rendering of td as A-Kohar has pointed out. Check your browser's dev-tools to ensure that your td looks like how you want it to be. Hopefully this helped.
0

You have an extra }); here

$( "body" ).on( "click", ".js-delete-log", function(event) {
    var el = $(this);
    var log_id = el.data('log-id');

          $.post( "../ajax/delete_logs.php", {log_id:log_id,action:'delete_log'} ); //You missed the semicolon here but it's not creating the issue                        
          location.reload();
    //}); Remove this line

});

Comments

0

There is a extra }); in your code, this should work

$(function(){
    $( "body" ).on( "click", ".js-delete-log", function(event) {
       // your code here....
    });

});

Comments

0

try this code

$(document).on( "click", ".js-delete-log .delete", function(e) {
    var el = $(this);
    var log_id = el.parent().data('log-id');
    alert(log_id);             
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.