0

I have this code (jquery):

function somefunction() {

        $.ajax({
        type : 'POST',
        url : 'post.php',
        dataType : 'json',

        success : function(data){

            var mess = "";  
            var count = 0;

            while (count < (data.length - 1))
            {

                 mess = mess + "<a href=# onclick=deletePerson("JohnDoe");return false;><img src=x.gif></a>" + data[count].name + "<br />";
                 count++;
            }


            $('#mydiv').html(mess).fadeIn('fast');


        },
    });


}
function deletePerson(arg) {...}

When I run this, everything works fine. However, when I want to pass a variable (instead of "JohnDoe") with the onclick, it stops working:

var myvar = "JohnDoe";
mess = mess + "<a href=# onclick=delete(myvar);return false;><img src=x.gif></a>" + data[count].name + "<br />";

My editor tells me: "Unresolved variable or type".

Never had a problem with passing parameters, but with this onClick-thing it just doesn't work...

Anyone knows what I'm doing wrong?

Thank's a lot!

EDIT: Joseph's post fixed the JohnDoe problem (thanks!), however, when I put:

var myvar = data[count].name;

it stops working... any thoughts?

3
  • can you tell what's coming with data variable ? Commented Oct 23, 2011 at 18:35
  • Creating HTML and binding event handlers like this is error prone and therefore bad style. Consider using jQuery to do this. Commented Oct 23, 2011 at 18:42
  • this is a very bad way to bind events. If you're using jquery, you should use it: $('<a href="#">').click(...)... Commented Oct 23, 2011 at 19:12

2 Answers 2

1

You need to concatenate the variable in

myvar = "JohnDoe";
mess = mess + "<a href=# onclick=delete('"+myvar+"');return false;><img src=x.gif></a>" + data[count].name + "<br />";
Sign up to request clarification or add additional context in comments.

3 Comments

You are missing quotes around the argument.
Also maybe take the var off of "var myvar" so it becomes global, don't know where he/she is declaring it.
Ok, now it works with "JohnDoe". However, I need the var to be data[count].name. So when I put var myvar = data[count].name; (inside the while loop off course) it stops working again... The delete() function doesn't get called, nothing happens. Any thoughts?
0

delete is a reserved keyword in javascript, you should rename your function.

...
mess = mess + "<a href=# onclick=deletePerson('"+myvar+"');return false;><img src=x.gif></a>" + data[count].name + "<br />";
...
function deletePerson(name) {...}

3 Comments

My bad, the function isn't actually named delete, but the translation of "to delete" in my native language. Changed it for stackoverflow, but hadn't thought about the reserved-keyword-thing. Thanks for pointing this out, problem still unfixed though...
@binoculars Are you sure data is an array? Do a console.log() to check it out, maybe it's a string, or maybe the array elements don't have a name property. Also, you should update the code in your post so that it matches the current state.
I'm sure it's an array and it exists, since the while loop with data[count].name inside it works just fine... (update my code)

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.