0

I'm trying to add a row in a table from a data from another table. it says uncaught reference error.

here's the function

function addItem(id, name){
    bootbox.confirm("Are you sure?", function(result) {
        if (result) {
            $('#ingredients_table').append("<tr><td>"+name+"</td></tr>");
        }
    });
}

here's the table where I'm calling the js function

<?php foreach($inventory as $row): ?>
    <tr style="cursor: pointer;" data-toggle="tooltip" data-placement="top" title="Add ingredient" onclick="javascript:addItem(<?php echo $row->inventory_id; ?>, <?php echo $row->name; ?>)">
    <td><?php echo $row->name; ?></td>
</tr>
<?php endforeach; ?>

The ID is showing in the table when I switch them, but the name is not.

3
  • Show the rendered HTML results since that is what the function and jQuery work with. Commented Mar 27, 2018 at 10:30
  • since u are using php better enclose parameters in quotes like onclick="javascript:addItem('<?php echo $row->inventory_id;?>','<?php echo $row->name;?>')" Commented Mar 27, 2018 at 10:32
  • @VibinTV that solved the problem, seems that it needs quotes. Thanks man! Commented Mar 27, 2018 at 10:34

1 Answer 1

2

Hope this will help you :

use '' (quotes) for javascript function arguments

 <?php foreach($inventory as $row): ?>
    <tr style="cursor: pointer;" data-toggle="tooltip" data-placement="top"
        title="Add ingredient" 
        onclick="javascript:addItem('<?php echo $row->inventory_id; ?>', '<?php echo $row->name; ?>')">

   <td><?php echo $row->name; ?></td>

Sign up to request clarification or add additional context in comments.

2 Comments

glad it helps you , happy coding
@Alvinrightback don't forget to accept answer if solved for you.

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.