1

I have completed if I pass a variable id but now I want to pass name to the same function when I click on a image. This is my code:

 for (var i = 0; i <friend_data.length; i++) {
                        results += '<div class = "clicker" id = "'+friend_data[i].id+'" onclick="javascript:testId(this.id,'+ friend_data[i].name+')"><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture" height="30" width="30">' + friend_data[i].name + '</div>';
                    }

and

function testId(id, friend_data[id].name ){
    alert(id);
    alert(friend_data[i].name);     
}

The alert(friend_data[i].name); does not show because it's wrong. Please help me to correct it. Please look in even onclick it's the right ways to pass variable?

5
  • No, it's alert only ID value but when I give it's alert name, It's errors Commented Apr 19, 2013 at 10:15
  • friend_data[id].name is not a valid parameter name. (mothereff.in/js-variables) Commented Apr 19, 2013 at 10:18
  • friend_data[id].name is show my friend name is sure if I call in correct ways because I used it to show name of my fecebook's friend again already. Commented Apr 19, 2013 at 10:26
  • You need to change the variable name only in your function. Commented Apr 19, 2013 at 10:28
  • please help me to solve it. I know my code is not correct but I want to show it to everybody for see it and help me to modifier it. Commented Apr 19, 2013 at 10:32

5 Answers 5

4

try this

function testId(id, name ){
    alert(id);
    alert(name);
}
Sign up to request clarification or add additional context in comments.

Comments

3

Change for loop to this

for (var i = 0; i <friend_data.length; i++) {
results += '<div class = "clicker" id = "'+friend_data[i].id+'" onclick="javascript:testId(this.id,'+friend_data[i].name+')"><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture" height="30" width="30">' + friend_data[i].name + '</div>';
}

6 Comments

While I add + to my code but I still get the same result, it's can't get name values
Did u change ur function function testId(id, friend_data[id].name ){ to function (id, name) ?
No I am not. It's still function testId(id, friend_data[id].name ){ alert(id); alert(friend_data[i].name); }
change it to function testId(id,name) { alert(id); alert(name); }
I don't know why it's nothing is happened. but when I view it in firebug add-on , it's show SyntaxError: missing ) after argument list [Break On This Error] javascript:testId(this.id,Chan Vireak) that when I checked it's not miss ) . other one it's show ìd` value it's stop show. right ?
|
2

You forgot to add + in the code '+friend_data[i].name+':

for (var i = 0; i <friend_data.length; i++) {
    results += '<div class = "clicker" id = "'+friend_data[i].id+'" onclick="javascript:testId(this.id, '+friend_data[i].name+')"><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture" height="30" width="30">' + friend_data[i].name + '</div>';
}

1 Comment

Yes, after I add + to my code but I am still can't get name value to function that I pass too .
0

Your function should be defined as:

function testId(id, name) {
    alert(id);
    alert(name);
}

name will take the value of friend_data[i].name, when the function is called.

Comments

0
function testId(id, name ){
    alert(id);
    alert(name);
}

And then call it with: testId(friend_data[i].id, friend_data[i].name)

Or

function testId(friend){
    alert(friend.id);
    alert(friend.name);
}

Adnd then call it with: testId(friend_data[i])

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.