I have pleted 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="/' + 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?
I have pleted 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./' + 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?
- No, it's alert only ID value but when I give it's alert name, It's errors – pov Commented Apr 19, 2013 at 10:15
-
friend_data[id].name
is not a valid parameter name. (mothereff.in/js-variables) – Anirudh Ramanathan 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. – pov Commented Apr 19, 2013 at 10:26
- You need to change the variable name only in your function. – Anirudh Ramanathan 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. – pov Commented Apr 19, 2013 at 10:32
5 Answers
Reset to default 4try this
function testId(id, name ){
alert(id);
alert(name);
}
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./' + friend_data[i].id + '/picture" height="30" width="30">' + friend_data[i].name + '</div>';
}
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./' + friend_data[i].id + '/picture" height="30" width="30">' + friend_data[i].name + '</div>';
}
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.
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])