473,804 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass ID to function?

5 New Member
I'm attempting display a DIV when clicking on a list item. This works, but only for the first list item. I realized that this was due to the ID not being unique so I made the IDs unique. My question is how do I pass this unique ID from each list item to the relevant javascript function so that it displays the DIV when clicked?

Here's the javascript:
Expand|Select|Wrap|Line Numbers
  1. // Create XmlHttp object.
  2. function createXHR()
  3. {
  4.     try { return new XMLHttpRequest(); } catch(e) {}
  5.     try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
  6.     try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
  7.     try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  8.     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  9.     alert("XMLHttpRequest not supported");
  10.     return null;
  11. }
  12.  
  13. function sendRequest()
  14. {
  15.     var xhr = createXHR(); // cross browser XHR creation
  16.     if (xhr) // if created run request
  17.     {
  18.         xhr.open("GET","moreContent.php",true);
  19.         xhr.onreadystatechange = function(){handleResponse(xhr);};
  20.         xhr.send(null);
  21.     }
  22. }
  23.  
  24. function handleResponse(xhr)
  25. {
  26.     if (xhr.readyState == 4  && xhr.status == 200)
  27.     {
  28.         var responseOutput = document.getElementById("responseOutput");
  29.         responseOutput.innerHTML = xhr.responseText;
  30.         responseOutput.style.display = "";
  31.     }
  32. }
  33.  
  34. window.onload = function () 
  35. {
  36.     document.getElementById(uniqueId).onclick = sendRequest;   
  37. }
The HTML:
Expand|Select|Wrap|Line Numbers
  1.         foreach($rs as $k => $row)
  2.         {
  3.  
  4.             if (checkFlag($row[flags], TEST_FAILED))
  5.             {
  6.                 echo "<li id=\"failContent\">";
  7.             }
  8.             else
  9.             {
  10.                 echo "<li id=\"$row[id]\">";
  11.             }
  12.  
  13.                 echo "Summary data";
  14.  
  15.                 echo "<div id=\"responseOutput\"></div>";
  16.  
  17.             echo "</li>";
  18.  
  19.             echo "\n";
  20.         }
  21.  
Jul 31 '08 #1
7 2113
acoder
16,027 Recognized Expert Moderator MVP
You need to add an onclick to each element. You can add that by getting all the elements via document.getEle mentsByTagName( "li") and looping over them to add the onclick. To get the target of the event, see this link.
Jul 31 '08 #2
renphi
5 New Member
It now works after changing the following:
Expand|Select|Wrap|Line Numbers
  1. window.onload = function () 
  2. {
  3.     var arr = document.getElementsByTagName("li");
  4.     for(var i = 0; i < arr.length; i++)
  5.     {
  6.         document.getElementsByTagName("li")[i].onclick = sendRequest;       
  7.     }
  8. }
  9.  
And added:
Expand|Select|Wrap|Line Numbers
  1. var theId
  2. function getId(e)
  3. {
  4.     var targ;
  5.     if (!e)
  6.     {
  7.         var e = window.event;
  8.     }
  9.     if (e.target)
  10.     {
  11.         targ = e.target;
  12.     }
  13.     else if (e.srcElement)
  14.     {
  15.         targ = e.srcElement;
  16.     }
  17.     if (targ.nodeType == 3) // defeat Safari bug
  18.     {
  19.         targ = targ.parentNode;
  20.     }
  21.     theId=targ.id
  22.     //alert(theId)
  23. }
  24.  
  25. document.onclick = getId;
  26.  
How difficult would it be to modify this now to hide/show this DIV? Thanks for your suggestions.
Jul 31 '08 #3
renphi
5 New Member
I may have spoken too soon. While the following code works well in Firefox, it fails with large sets of data in Internet Explorer 6 & 7 pinning the CPU at 100%. Is there something in there causing this issue?

Expand|Select|Wrap|Line Numbers
  1. // Create XmlHttp object.
  2. function createXHR()
  3. {
  4.     try { return new XMLHttpRequest(); } catch(e) {}
  5.     try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
  6.     try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
  7.     try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  8.     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  9.     alert("XMLHttpRequest not supported");
  10.     return null;
  11. }
  12.  
  13. function sendRequest()
  14. {
  15.     var xhr = createXHR(); // cross browser XHR creation
  16.     if (xhr) // if created run request
  17.     {
  18.         url = "moreContent.php?id=" + theId;
  19.         xhr.open("GET",url,true);
  20.         xhr.onreadystatechange = function(){handleResponse(xhr);};
  21.         xhr.send(null);
  22.     }
  23. }
  24.  
  25. function handleResponse(xhr)
  26. {
  27.     if (xhr.readyState == 4  && xhr.status == 200)
  28.     {
  29.         newId = "div" + theId;
  30.         var responseOutput = document.getElementById(newId);
  31.         responseOutput.innerHTML = xhr.responseText;
  32.         responseOutput.style.display = "";
  33.     }
  34. }
  35.  
  36. window.onload = function () 
  37. {
  38.     var arr = document.getElementsByTagName("li");
  39.     for(var i = 0; i < arr.length; i++)
  40.     {
  41.         document.getElementsByTagName("li")[i].onclick = sendRequest;       
  42.     }
  43. }
  44.  
  45. var theId
  46. function getId(e)
  47. {
  48.     var targ;
  49.     if (!e)
  50.     {
  51.         var e = window.event;
  52.     }
  53.     if (e.target)
  54.     {
  55.         targ = e.target;
  56.     }
  57.     else if (e.srcElement)
  58.     {
  59.         targ = e.srcElement;
  60.     }
  61.     if (targ.nodeType == 3) // defeat Safari bug
  62.     {
  63.         targ = targ.parentNode;
  64.     }
  65.     theId=targ.id
  66.     //alert(theId)
  67. }
  68.  
  69. document.onclick = getId;
  70.  
Jul 31 '08 #4
renphi
5 New Member
Upon further inspection the following function is causing Internet Explorer to hang.

Expand|Select|Wrap|Line Numbers
  1. window.onload = function () 
  2. {
  3.     var arr = document.getElementsByTagName("li");
  4.     for(var i = 0; i < arr.length; i++)
  5.     {
  6.         document.getElementsByTagName("li")[i].onclick = sendRequest;       
  7.     }
  8. }
  9.  
This is getting seriously frustrating. If anyone had any idea as to why the function would be causing IE 6 & 7 to hang please let me know. Firefox has no issue...
Jul 31 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
How many list items do you have?
Aug 1 '08 #6
renphi
5 New Member
How many list items do you have?
Varies, but in this case ~15,000.
Aug 1 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
Well, that is a lot. That's probably what's causing it to hang.

Try including the onclick inlinea and see if that makes a difference.
Aug 1 '08 #8

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

Similar topics

7
1830
by: EsC | last post by:
Hy! is it possible to pass function-arguments by reference? (for example in PHP you can use the "&" operator ... ) thx iolo
10
1468
by: Ron_Adam | last post by:
I'm trying to figure out how to test function arguments by adding a decorator. @decorate def func( x): # do something return x This allows me to wrap and replace the arguments with my own, but not get the arguments that the original function received.
39
6570
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
4
15029
by: R Reyes | last post by:
trying to shorten up code by passing a function name as a parameter. Here is what it looks like: function doSomething(function myFunctionName()) { // some code like: // DB.createAdapter //DB.attachAdapter // DB.isOpen // do this function - which does something different every time
5
1189
by: BabyBlue | last post by:
I have a function like this: function get_articles($cat=1,$numberposts=1); it can be used: get_articles(cat=4&numberposts=6); //will display 6 posts from category 6 then I want to use that function within another function, like this: function display_cat($new_cat=1,$new_num=1,$othercontent="")
3
2543
by: jimhce | last post by:
Hi, How can a child class passes its non-static member function to the parent class? class A { public: typedef void *(*object_t)(void *); A(object_t f) : func(f) {} virtual ~A(){} private:
4
1565
by: demonbunny666 | last post by:
I'm trying to get this program to work, but I am running into a problem and as hard as I try I can't figure it out. It compiles fine( VS Studio 6.0) but when it executes a problem occurs, something having to do with an "Unhandled exception:access violation". I'm new to vectors, and from what I can tell I did something wrong with the 2-dim vector, becuase my functions with single dim vectors work fine. #include<vector> #include<iostream>...
7
8438
by: dries | last post by:
Hello lads, I'd like to do the following in C: 1. The user writes a function with predefined arguments and return value in a separate file. 2. The program is compiled as my written 'main' program and the file containing the user function included. 3. When the program is invoked, the user is asked how his function is named (f.e. function1). 4. The 'main' program calls and executes the user function. The problem is, how can I cast the...
10
5110
by: Olaf Dietrich | last post by:
I may just be temporarily (hopefully ...) stupid, but how can I pass a function pointer between functions using an array of (signed/unsigned) chars (in a standard-conforming way)? E.g.: I have a function: int (*func)(double, int). Now I'd like to store the "address" of this function
4
15790
by: soni2926 | last post by:
hi, is it possible to pass a function into another function as a parameter? Say i have these: function SaveMe(text) {...} function SaveMeNow(text) {...}
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.