473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

onclick error with function call and passing values

I am trying to get an onclick event to pass some values to a function to display in an alert box for now. I can get it sort of working but now the way I need it. I will be changing the alert in the function to open in a new window after I get the onclick call working properly.

I read from an array to get my names then I want to click on the link to get the rest of the info for that person.

If you change the for loop line to be from names_array.length to names_array.length-1
the values of the last item in the arrays get passed to the function and it does not matter what link gets clicked. I need it to pass the correct values from the correct line that gets clicked.

Any help anyone can give me would be greatly apprieciated

Thanks Glen



CODE
<script language="javascript" type="text/javascript">
<!--

Expand|Select|Wrap|Line Numbers
  1. // Names parallel Array 
  2.  
  3. var names_array = new Array(); 
  4.  
  5. names_array[0] = 'Bob'; 
  6. names_array[1] = 'Sally'; 
  7. names_array[2] = 'Henry'; 
  8. names_array[3] = 'Gerry'; 
  9. names_array[4] = 'Betty'; 
  10.  
  11.  
  12.  
  13. // Phone parallel Array 
  14.  
  15. var phone_array = new Array(); 
  16.  
  17. phone_array[0] = '555-1111'; 
  18. phone_array[1] = '555-2222'; 
  19. phone_array[2] = '555-3333'; 
  20. phone_array[3] = '555-4444'; 
  21. phone_array[4] = '555-5555'; 
  22.  
  23.  
  24. // Address parallel Array 
  25.  
  26. var address_array = new Array(); 
  27.  
  28. address_array[0] = 'Vancouver\, BC'; 
  29. address_array[1] = 'Calgary\, Alberta'; 
  30. address_array[2] = 'Saskatoon\, Sask'; 
  31. address_array[3] = 'Winnipeg\, Manitoba'; 
  32. address_array[4] = 'Toronto\, Ont'; 
  33.  
  34.  
  35. for (i = 0; i < names_array.length; i++) { 
  36.  
  37. document.write('<a href="#" onclick="passNames(names_array[i], phone_array[i], address_array[i]);">Click to see Info ' + names_array[i] + ' \<\/a>'); 
  38. document.write("<hr />"); 
  39.  
  40.  
  41. function passNames(name1,name2,name3) { 
  42.  
  43. alert(name1 + " - " + name2 + " " + name3); 
  44.  
-->
</script>

END CODE
Feb 15 '06 #1
2 3054
I am trying to get an onclick event to pass some values to a function to display in an alert box for now. I can get it sort of working but now the way I need it. I will be changing the alert in the function to open in a new window after I get the onclick call working properly.

I read from an array to get my names then I want to click on the link to get the rest of the info for that person.

If you change the for loop line to be from names_array.length to names_array.length-1
the values of the last item in the arrays get passed to the function and it does not matter what link gets clicked. I need it to pass the correct values from the correct line that gets clicked.

Any help anyone can give me would be greatly apprieciated

Thanks Glen



CODE
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript"> 
  2. <!-- 
  3.  
  4. // Names parallel Array 
  5.  
  6. var names_array = new Array(); 
  7.  
  8. names_array[0] = 'Bob'; 
  9. names_array[1] = 'Sally'; 
  10. names_array[2] = 'Henry'; 
  11. names_array[3] = 'Gerry'; 
  12. names_array[4] = 'Betty'; 
  13.  
  14.  
  15.  
  16. // Phone parallel Array 
  17.  
  18. var phone_array = new Array(); 
  19.  
  20. phone_array[0] = '555-1111'; 
  21. phone_array[1] = '555-2222'; 
  22. phone_array[2] = '555-3333'; 
  23. phone_array[3] = '555-4444'; 
  24. phone_array[4] = '555-5555'; 
  25.  
  26.  
  27. // Address parallel Array 
  28.  
  29. var address_array = new Array(); 
  30.  
  31. address_array[0] = 'Vancouver\, BC'; 
  32. address_array[1] = 'Calgary\, Alberta'; 
  33. address_array[2] = 'Saskatoon\, Sask'; 
  34. address_array[3] = 'Winnipeg\, Manitoba'; 
  35. address_array[4] = 'Toronto\, Ont'; 
  36.  
  37.  
  38. for (i = 0; i < names_array.length; i++) { 
  39.  
  40. document.write('<a href="#" onclick="passNames(names_array[i], phone_array[i], address_array[i]);">Click to see Info ' + names_array[i] + ' \<\/a>'); 
  41. document.write("<hr />"); 
  42.  
  43.  
  44. function passNames(name1,name2,name3) { 
  45.  
  46. alert(name1 + " - " + name2 + " " + name3); 
  47.  
  48. --> 
  49. </script> 
END CODE
Feb 15 '06 #2
acoder
16,027 Expert Mod 8TB
The array items need to be quoted, but watch out for quotes within quotes:
Expand|Select|Wrap|Line Numbers
  1. document.write('<a href="#" onclick="passNames(\''+names_array[i]+'\', \''+phone_array[i]+'\', \''+address_array[i]+'\');">Click to see Info ' + names_array[i] + ' \<\/a>'); 
Apr 16 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: bob | last post by:
Hi, consider this script: <script> function doIt() { alert("ok") } document.onclick=doIt // or document.onclick=doIt() </script>
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
2
by: Galsaba | last post by:
How can I send back an argumet with "onClick"? <a href=test.php onClick="checkLink ('Gallon')" > 1 Gallon</a> function checkLink(a) { return (a+3) } When the user click on the word "Gallon"...
1
by: Dark Magician | last post by:
Comrades: Am trying to build a UI widget. I'm sure part of the problem is proper variable scope or object reference, and part of the problem may be the way I'm calling the function, but, here...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: Neven Klofutar | last post by:
Hi, I need to create the following ... I have DataGrid filled with information (ID, name, price ...). I need to call a function when I click on the item in the DataGrid (and pass ID as an...
5
by: Fred.Grieco | last post by:
Hi every body, I have a little pb and I'm turning around : function MyFCTN(var1,var2) { var mytable = document.getElementById("myTBL"); for (var i=myTBL.childNodes.length-1; i>0; i--){...
17
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html http://www.frostjedi.com/terra/scripts/demo/this-alert2.html Why, when you click in the black box, do the alert boxes say different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.