0

enter image description here I have a an xml file that I'm using as a database for my website. I have a table that contains all the products of the website. When you add a product to the xml, it automatically updates the table (see image).

Now, I want to be able to delete the product from here also by clicking the delete button. I want to delete the product by grabbing the name of the product by Javascript and sending that name back to php for processing the xml.

Here's the problem: When I try to grab the product name from the table, the string is empty when I do console.log. How can I solve this?

Here is some relevant code:

This is the part of the Javascript where I am reading the xml file to append the info of the product from xml to the website:

                //name
                td = document.createElement('td');
                td.innerHTML = productNode.children[0].innerHTML;
                row.appendChild(td);

                //price
                td = document.createElement('td');
                let price = productNode.children[4].innerHTML;
                let priceArray = price.split(',');
                let priceList = makeUL(priceArray);
                td.appendChild(priceList);
                row.appendChild(td);

                //type 
                td = document.createElement('td');
                let type = productNode.children[6].innerHTML;
                let typeArray = type.split(',');
                let typeList = makeUL(typeArray);
                td.appendChild(typeList);
                row.appendChild(td);

                console.log("running..........")


                //size
                td = document.createElement('td');
                let size = productNode.children[5].innerHTML;
                let sizeArray = size.split(',');
                let sizeList = makeUL(sizeArray);
                td.appendChild(sizeList);
                row.appendChild(td);

                //quantity
                td = document.createElement('td');
                td.innerHTML = productNode.children[2].innerHTML;
                row.appendChild(td);

                //stock
                td = document.createElement('td');
                td.innerHTML = productNode.children[3].innerHTML;
                row.appendChild(td);


                //buttons to remove and edit (not pulled from database)
                td = document.createElement('td');
                td.innerHTML = "<a href='edit_product.html' class='btn btn-outline-dark'>Edit</a>  <a href='#myModal' name='delete' onclick=deleteProduct() class='btn btn-outline-danger' >Remove</a>"
                row.appendChild(td);
            
                productTable.children[1].appendChild(row); 

And here is the Javascript for sending info to the server:

 function deleteProduct() {
    let productName = $(this).closest('tr').find('td:nth-child(1)').text();
    console.log(productName);
    $.post("deleteProduct.php", {prodName : productName});
} 

In this segment, when I do console.log(productName), it doesn't print anything, hence the server can't process it.

3
  • 3
    Inline bindings do not have context associated with them. console.log(this) to show this. Commented Aug 11, 2020 at 22:28
  • 4
    Create a real event listener on the elements, or change it to onclick="deleteProduct(this)" and pass the link into the method. Commented Aug 11, 2020 at 22:29
  • can you write it in the answer with an example? Commented Aug 11, 2020 at 22:30

1 Answer 1

3

The quick and dirty way to fix this is to pass the element in on the method call.

onclick="deleteProduct(this)"

And then your method would accept the link in as an argument and use it.

function deleteProduct(link) {
    let productName = $(link).closest('tr').find('td:nth-child(1)').text();
    console.log(productName);
    $.post("deleteProduct.php", {prodName : productName});
} 

However, the preferred way would be to either make an actual event handler on the elements, which would have context, or to make a delegate event handler higher up on a shared parent of all the elements that would perform the logic.

jQuery Direct Bindings: https://learn.jquery.com/events/event-basics/

jQuery Delegate Event Bindings: https://learn.jquery.com/events/event-delegation/

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

4 Comments

thank you, but how would you use the element that is passed inside the deleteProduct() function? Sorry, i'm new to this...
I just added the function snippet.
ok thank you, but it still seems to print out nothing
You will need to debug what $(link).closest('tr').find('td:nth-child(1)') is finding and verify that it is the correct element.

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.