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 : - // Create XmlHttp object.
-
function createXHR()
-
{
-
try { return new XMLHttpRequest(); } catch(e) {}
-
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
-
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
-
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
-
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
-
alert("XMLHttpRequest not supported");
-
return null;
-
}
-
-
function sendRequest()
-
{
-
var xhr = createXHR(); // cross browser XHR creation
-
if (xhr) // if created run request
-
{
-
xhr.open("GET","moreContent.php",true);
-
xhr.onreadystatechange = function(){handleResponse(xhr);};
-
xhr.send(null);
-
}
-
}
-
-
function handleResponse(xhr)
-
{
-
if (xhr.readyState == 4 && xhr.status == 200)
-
{
-
var responseOutput = document.getElementById("responseOutput");
-
responseOutput.innerHTML = xhr.responseText;
-
responseOutput.style.display = "";
-
}
-
}
-
-
window.onload = function ()
-
{
-
document.getElementById(uniqueId).onclick = sendRequest;
-
}
The HTML: -
foreach($rs as $k => $row)
-
{
-
-
if (checkFlag($row[flags], TEST_FAILED))
-
{
-
echo "<li id=\"failContent\">";
-
}
-
else
-
{
-
echo "<li id=\"$row[id]\">";
-
}
-
-
echo "Summary data";
-
-
echo "<div id=\"responseOutput\"></div>";
-
-
echo "</li>";
-
-
echo "\n";
-
}
-
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.
It now works after changing the following: -
window.onload = function ()
-
{
-
var arr = document.getElementsByTagName("li");
-
for(var i = 0; i < arr.length; i++)
-
{
-
document.getElementsByTagName("li")[i].onclick = sendRequest;
-
}
-
}
-
And added: -
var theId
-
function getId(e)
-
{
-
var targ;
-
if (!e)
-
{
-
var e = window.event;
-
}
-
if (e.target)
-
{
-
targ = e.target;
-
}
-
else if (e.srcElement)
-
{
-
targ = e.srcElement;
-
}
-
if (targ.nodeType == 3) // defeat Safari bug
-
{
-
targ = targ.parentNode;
-
}
-
theId=targ.id
-
//alert(theId)
-
}
-
-
document.onclick = getId;
-
How difficult would it be to modify this now to hide/show this DIV? Thanks for your suggestions.
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? -
// Create XmlHttp object.
-
function createXHR()
-
{
-
try { return new XMLHttpRequest(); } catch(e) {}
-
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
-
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
-
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
-
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
-
alert("XMLHttpRequest not supported");
-
return null;
-
}
-
-
function sendRequest()
-
{
-
var xhr = createXHR(); // cross browser XHR creation
-
if (xhr) // if created run request
-
{
-
url = "moreContent.php?id=" + theId;
-
xhr.open("GET",url,true);
-
xhr.onreadystatechange = function(){handleResponse(xhr);};
-
xhr.send(null);
-
}
-
}
-
-
function handleResponse(xhr)
-
{
-
if (xhr.readyState == 4 && xhr.status == 200)
-
{
-
newId = "div" + theId;
-
var responseOutput = document.getElementById(newId);
-
responseOutput.innerHTML = xhr.responseText;
-
responseOutput.style.display = "";
-
}
-
}
-
-
window.onload = function ()
-
{
-
var arr = document.getElementsByTagName("li");
-
for(var i = 0; i < arr.length; i++)
-
{
-
document.getElementsByTagName("li")[i].onclick = sendRequest;
-
}
-
}
-
-
var theId
-
function getId(e)
-
{
-
var targ;
-
if (!e)
-
{
-
var e = window.event;
-
}
-
if (e.target)
-
{
-
targ = e.target;
-
}
-
else if (e.srcElement)
-
{
-
targ = e.srcElement;
-
}
-
if (targ.nodeType == 3) // defeat Safari bug
-
{
-
targ = targ.parentNode;
-
}
-
theId=targ.id
-
//alert(theId)
-
}
-
-
document.onclick = getId;
-
Upon further inspection the following function is causing Internet Explorer to hang. -
window.onload = function ()
-
{
-
var arr = document.getElementsByTagName("li");
-
for(var i = 0; i < arr.length; i++)
-
{
-
document.getElementsByTagName("li")[i].onclick = sendRequest;
-
}
-
}
-
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...
acoder 16,027
Recognized Expert Moderator MVP
How many list items do you have?
How many list items do you have?
Varies, but in this case ~15,000.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
|
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...
|
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
|
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="")
| |
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:
|
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>...
|
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...
|
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
|
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)
{...}
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |