Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trap event "onClick" on <a href=...... onClick=> 1

Status
Not open for further replies.

DoTheLoop

Programmer
Sep 12, 2003
14
0
0
SE
Hi again!

I'm trying to figure out how to check which link the user has clicked on.

Goes like this:
<html>
<script>
function trapper() {
var collAElements = document.body.getElementsByTagName(&quot;A&quot;);
for (i=0;i<collAElements.length;i++) {
//trying to find which one the user has clicked???
}
</script>
<body>
<form>
<input type=text name=xxx value=yyy>
<a href=&quot;&quot; onclick=javascript:trapper();>Change value in xxx</a>
<input type=text name=zzz value=yyy>
<a href=&quot;&quot; onclick=javascript:trapper();>Change value in zzzz/a>
etc
..
..
..
</form>
</body>
</html>

What I'm actually trying to do here is determine which of this links the user has clicked without knowing any values or names on the form.

Cheers
DoTheLoop
 
I think this would work:

<a href=&quot;&quot; onclick=javascript:trapper(&quot;box1&quot;);>

and then your trapper() script would be something like this:

function trapper(testBox) {
if (testBox == &quot;box_1&quot;) {
do something
.
.
.
}}

There's always a better way. The fun is trying to find it!
 
Thanks for the reply.

But in this case you would need to name the &quot;box1&quot; uniquely for each <a href=&quot;&quot; ...> you have on the page. In this case I haven't got that luxury (yet). The only thing I have is a row of <a href=&quot;&quot;...>'s and they have no significant (sofar) identifier except the fact that they at a given moment have been clicked.

Is it at all possible to scan through the array of <a href=...>'s on my page and check some property on every single one of them and determine if they have been clicked?

Please some input on this would be most welcome.
 
>> Please some input on this would be most welcome

Naturally, You wouldn't have posted otherwise... I think the major stumbling block here is that no-one really knows what you are trying to achieve. From what I can gather, you need to get a handle on an <a> when it is clicked. Some idea of what you need it for, and what you plan on doing with it when you have it may make it a little easier to help you out.

Do you need to keep a record of all the links that have been clicked in this session? Do you just need to access the link that has most recently been clicked so that you can do some processing before directing the user to the page?

From you first post it looks like for each link that the user clicks on, some value is placed in a text box. What did you hope to put in the box?

There is no property of the <a> object that indicates if it has been clicked or not. However, depending on your browser you could either - create your own property, or, bend an existing property - such as the class - to suit your purposes. And you can certainly loop through any of the document.all, document.anchors or document.links collections to get at them.

Sorry we can't be more helpful at the moment, but you haven't given us a lot to work with here ;-)

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Sorry if was unclear of what I was trying to achieve.
Thanks dwarfthrower for pointing that out.

My problem is tricky and I'm getting more convinced that it won't work.

Well here is the whole story:
First : Luckily (or unluckily depending on the current politics regarding which browser is &quot;best&quot;) the only browser I have to worry about in this intranet is IE6.0.

I have a single page with 1 form that is created &quot;on the fly&quot; from an portalengine, ie. I have no control on how the page is formatted, how many elements (<a href=...>'s, <input type=...>'s ....etc), the control of the layout is (sofar) hidden from me. In this particular case I have the problem that the portalengine throws out a page with duplicate tagnames. This seem's rather stupid, but with the automation of creating html the problem of having some intelligence (javascript) on the page sent to the user (in this case an intranet) may have occurred before.

Well. Duplicate - triplicate - tagnames of the same type of element (<a href=...> on the page is my problem. And &quot;YES&quot; dwarfthrower <quote>&quot;Do you just need to access the link that has most recently been clicked so that you can do some processing before directing the user to the page?&quot;</quote>
That is very accurate. Also <quote>&quot;From you first post it looks like for each link that the user clicks on, some value is placed in a text box. What did you hope to put in the box?&quot;</quote>. The answer to this is that when I have determined which <a href=...> is clicked I open a new window and there the user can change some data, close that window and my javascript put's the changes in a textbox, preferably the &quot;closest&quot; one to the <a href=...>.

Shortly put this problem is trying to find out a way to keep track of the arrays created by DOM and what element in the array is being manipulated by the user at this very moment. Then send that information - along with other information - to the popupwindow.

Does this explanation make it clearer?

Regards DoTheLoop
 
I was thinking of looping through the links and adding an onclick event on the fly, but I can't get the 'this' reference to work in that context.

I'll post it up if I can figure it out.
 
Okay, I can add the equivalent of an inline
Code:
onclick=&quot;someFunction( this );&quot;
to the links dynamically now. (IE and Mozilla only.)

Let me know if that'll help you and I'll post up some code. You want to generate a popup I gather. What exactly do you want to send to the popup window?
 
Having bumped it you could at least check the thread, eh. :)
 
Have a look at this:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>Link Reporter</title>
<script type=&quot;text/javascript&quot;>
function reportLink(objA) {
	alert(objA.href);
}
</script>
</head>
<body>
<a href=&quot;somepage.html&quot; onclick=&quot;reportLink(this);&quot;>to somepage.html</a><br>
<a href=&quot;someotherpage.html&quot; onclick=&quot;reportLink(this);&quot;>to someotherpage.html</a>
</body>
</html>
Tested in IE6 and NS7. The &quot;this&quot; refers to the element that 'owns' the onclick, in this case the <a>. From here I'm certain you can do what you're trying to do (perhaps with theboyhope's way of adding onclick= to the <a> elements :))

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Sorry theboyhope was weekend here.

Well the popup is going to do some checking to wether the parent is still there, if it has been reloaded...etc, but it's main function is to recieve values from parent. These values are to be tampered with and sent back to parent closing and sofort for all the <a href=...>'s on this page.

Thank you theboyhope and clarkin for &quot;this&quot; ....hehe :) input.

I will test this now.
 
theboyhope!

I want to open a popup with some values that is currently on the parent page. Along with this I must somehow send the &quot;this&quot; I guess. I'm working with the arrays (< href=...>) created by DOM and I must remember which one in the array to be able to point on exactly the <a href=...> that triggered the child I'm currently working with. Because - as mentioned before in this thread - the <input type=text ...> all have the same name and my reference to those are the <a href=..>.

And YES I want some code if you have it and want to share.
 
Well, I'm still not sure if the code I made up does what you need it to do, and there are certainly easier ways to do this, but you mentioned at the start that you wanted to loop through the links. Anyways, here goes ...

Code:
<script>

window. onload = function() {

   var aElements = document.getElementsByTagName( &quot;a&quot; );

   for ( i=0; i < aElements.length; i++ ) {
 
      addEvent( aElements[i], &quot;click&quot;, testFunction, true );

   }
}

function addEvent( obj, event_type, fn, capture ) {

	if ( obj.addEventListener ) { 

		obj.addEventListener( event_type, function () { return testFunction( obj ); }, capture ); 
		return true; 

	} else if ( obj.attachEvent ) { 

		var r = obj.attachEvent( &quot;on&quot; + event_type, function () { return testFunction( obj ); } ); 
		return r;
	}
}

function testFunction( obj ) {

	document.body.innerHTML
		+= &quot;<p>&quot; + obj.href + &quot;</p>&quot;;
}

</script>

<body>
  <a href=&quot;#&quot;>Link 1</a>
  <a href=&quot;#&quot;>Link 2</a>
  <a href=&quot;#&quot;>Link 3</a>
</body>

Simply put, this code allows you to loop through all the anchor elements on a page and add an
Code:
onclick=&quot;someFunction( this );&quot;
event. That effectively gives you a reference to the link, which is passed to the function.

HTH. :)
 
My godness! Youre a sly fellow.

You are actually adding an event on thefly from javascript. I've never dreamed of doing that. This was a breakthrough. I can actually change the contents and apperance of that damned <a href=...>'s when I'm loading the document. Never thought of that. *slapping my head*

THANK YOU VERY MUCH :))))))

Big star for you theboyhope.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top