最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Passing two variables to one JavaScript function - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Apr 19, 2013 at 10:58 pov asked Apr 19, 2013 at 10:11 povpov 112 silver badges6 bronze badges 5
  • 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
Add a ment  | 

5 Answers 5

Reset to default 4

try 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])

发布评论

评论列表(0)

  1. 暂无评论