0

That's my code:

afficherListe();
function afficherListe()
{
 $.post('afficher_liste.php', function(data)
 { 
    var obj = jQuery.parseJSON(data);

    if(data!="")
    {
        $.each(obj, function(index, value){
            $('<li><button onclick="supprimer_article()">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));
        });
    }
    else
    {
        alert('no data');
    }
 }); 
}

function supprimer_article(article_selectionne)
{
    alert(article_selectionne);
}

Problem : I want to put my variable in supprimer_article() function, to have it in parameter and to continue my work in the function. Example : If I click on "X" Vodka button (view picture), I would find inalert(article_selectionne); "Vodka"

In a primary step, I've put the variable like that : $('<li><buttononclick="supprimer_article('+value.prixArticle+')">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));

But, the result in alert(article_selectionne); was : UNDEFINED

If you think you have the solution, it would be very nice and helpful to share it for me !

To have an idea of what do this function now (the final goal is that this function will remove the selected article from the DB after the onclick event....) :

enter image description here

2
  • I would upvote your question, but I cannot do that until your question is edited (these are rules of this community). So, if you edit your question in any way, I will upvote your question and you will have 15 reputations. Commented Mar 29, 2017 at 22:58
  • Okay, but which question are you talking about ? (my comment or my post) @FREE_AND_OPEN_SOURCE Commented Mar 31, 2017 at 12:34

2 Answers 2

1

You need to add name to function arguments when calling. Replace the line

$('<li><button onclick="supprimer_article()">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));  

with the following code:

$('<li><button onclick="supprimer_article(\'' + value.nomArticle + '\')">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));
Sign up to request clarification or add additional context in comments.

Comments

0

You need to place the value within the function itself. Change:

$('<li><button onclick="supprimer_article()">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));

to

$('<li><button onclick="supprimer_article('"+value.nomArticle+"')">X</button>'+value.nomArticle+' '+value.prixArticle+'</li>').insertAfter($('#liste_boissons'));

3 Comments

You need to hardcode value.nomArticle in DOM string, not like you did. What you did is wrong HTML syntax.
@FREE_AND_OPEN_SOURCE thanks for highlighting the error
Thank's for your responses, I tried your solution jeff but it isn't correct, it displays no data. @FREE_AND_OPEN_SOURCE I don't understand what I have to do for hardcode value.nomArticle in DOM string ?

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.