473,320 Members | 1,839 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,320 software developers and data experts.

Get id of clicked url

I'm sure this is simple and involves 'this' but my javascript powers
are not too hot.

From the url below how do I pick up the url's id in the function
taking into consideration this could be one of many urls?

<a href="javascript:void(0)" onclick="foo()" id="bar">

I thought this might have worked but it doesn't...

foo {
str = this.GetElementById
}

Cheers,

Chris
Aug 31 '08 #1
6 2424

"Chris" <ma*********@googlemail.comwrote in message
news:94**********************************@l42g2000 hsc.googlegroups.com...
I'm sure this is simple and involves 'this' but my javascript powers
are not too hot.

From the url below how do I pick up the url's id in the function
taking into consideration this could be one of many urls?

<a href="javascript:void(0)" onclick="foo()" id="bar">

I thought this might have worked but it doesn't...

foo {
str = this.GetElementById
}
window.event.srcElement.id;
Cheers,

Chris

Aug 31 '08 #2
On 31 Aug, 15:44, "MikeB" <m.byerleyATVerizonDottieNettiewrote:
"Chris" <matchett...@googlemail.comwrote in message

news:94**********************************@l42g2000 hsc.googlegroups.com...
I'm sure this is simple and involves 'this' but my javascript powers
are not too hot.
From the url below how do I pick up the url's id in the function
taking into consideration this could be one of many urls?
<a href="javascript:void(0)" onclick="foo()" id="bar">
I thought this might have worked but it doesn't...
foo {
str = this.GetElementById
}

window.event.srcElement.id;
Cheers,
Chris

Thanks Mike.

Chris
Aug 31 '08 #3
Chris wrote:
On 31 Aug, 15:44, "MikeB" <m.byerleyATVerizonDottieNettiewrote:
>"Chris" <matchett...@googlemail.comwrote in message:
>>I'm sure this is simple and involves 'this' but my javascript powers
are not too hot.
From the url below how do I pick up the url's id in the function
taking into consideration this could be one of many urls?
<a href="javascript:void(0)" onclick="foo()" id="bar">
Don't. <http://jibbering.com/faq/#FAQ4_42>
>>I thought this might have worked but it doesn't...
foo {
str = this.GetElementById
This cannot work. It is syntactically invalid (the identifier MUST be
preceded by the `function' keyword, and followed by a list of named
arguments, even if that is empty), pointless (`this' refers to the Global
Object in a globally called method, not the object handling the event),
error-prone (`str' was not declared a variable), and simply wrong even if
the correct object was being referred to (ECMAScript implementations are
case-sensitive, the method identifier is `getElementById', and it would not
be called, but referenced, here because the argument list was missing.)
>>}
window.event.srcElement.id;
That is IE-proprietary, and therefore nonsense in this general context. The
(so far) interoperable approach is the obvious one (which includes proper
syntax):

function foo(o)
{
var str = o.id;
}

<a href="..." onclick="foo(this)" id="bar">

However, it is likely then that you don't need the ID anymore as you have
the object element reference with `o' already.

Please RTFFAQ and RTFM before you post here again.
>[...]

Thanks Mike.
My name is not Mike, but you are welcome, I think.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Sep 1 '08 #4
On Aug 31, 3:44*pm, "MikeB" <m.byerleyATVerizonDottieNettiewrote:
"Chris" <matchett...@googlemail.comwrote in message

news:94**********************************@l42g2000 hsc.googlegroups.com...
I'm sure this is simple and involves 'this' but my javascript powers
are not too hot.
From the url below how do I pick up the url's id in the function
taking into consideration this could be one of many urls?
<a href="javascript:void(0)" onclick="foo()" id="bar">
I thought this might have worked but it doesn't...
foo {
str = this.GetElementById
}

window.event.srcElement.id;
Cheers,
Chris

Remember, window.event only works on Internet Explorer versions 5.0
onwards. What you need is a simple cross-browser solution like the
following:

function foo(e)
{
var sourceElm = (e.target) ? e.target : window.event.srcElement;
alert(sourceElm.id);
}

...where e is passed as an object of window.event in IE, but in
Firefox, it is passed as an implicit event object. IE uses the
window.event.srcElement routine to get to the calling object,
but in EOMB (every other modern browser), e.target is used to
retrieve the calling object.

You will call the above method simply like this:

<a id="bar" href="javascript:void(0);" onclick="foo(event)">Click
me</a>

Cheers,
Arun
Sep 1 '08 #5

"Arun" <a.********@gmail.comwrote in message
news:c4**********************************@p25g2000 hsf.googlegroups.com...
On Aug 31, 3:44 pm, "MikeB" <m.byerleyATVerizonDottieNettiewrote:
"Chris" <matchett...@googlemail.comwrote in message

news:94**********************************@l42g2000 hsc.googlegroups.com...
I'm sure this is simple and involves 'this' but my javascript powers
are not too hot.
From the url below how do I pick up the url's id in the function
taking into consideration this could be one of many urls?
<a href="javascript:void(0)" onclick="foo()" id="bar">
I thought this might have worked but it doesn't...
foo {
str = this.GetElementById
}

window.event.srcElement.id;
Cheers,
Chris

Remember, window.event only works on Internet Explorer versions 5.0
onwards. What you need is a simple cross-browser solution like the
following:

function foo(e)
{
var sourceElm = (e.target) ? e.target : window.event.srcElement;
alert(sourceElm.id);
}
>
Thanks for this. I don't do much browser scripting, but I did know of the
IE solution.
>
...where e is passed as an object of window.event in IE, but in
Firefox, it is passed as an implicit event object. IE uses the
window.event.srcElement routine to get to the calling object,
but in EOMB (every other modern browser), e.target is used to
retrieve the calling object.

You will call the above method simply like this:

<a id="bar" href="javascript:void(0);" onclick="foo(event)">Click
me</a>

Cheers,
Arun
Sep 2 '08 #6
On Sep 2, 1:05*am, Arun <a.regin...@gmail.comwrote:
[...]
Remember, window.event only works on Internet Explorer versions 5.0
onwards. What you need is a simple cross-browser solution like the
following:

*function foo(e)
*{
* *var sourceElm = (e.target) ? e.target : window.event.srcElement;
* *alert(sourceElm.id);
*}
Given your code below, e is a reference to window.event in IE
already. The usual pattern here is:

function foo(e) {
var e = e || window.event;
var tgt = e.target || e.srcElement;

/* do stuff with e and tgt */

}

Noting that tgt will be reference to the element that initiated the
event, which is not necessarily the element that has the listener
(consider a bubbling event from a span inside the A element).
[...]
..where e is passed as an object of window.event in IE, but in
Firefox, it is passed as an implicit event object. IE uses the
window.event.srcElement routine to get to the calling object,
but in EOMB (every other modern browser), e.target is used to
retrieve the calling object.

You will call the above method simply like this:

* <a id="bar" href="javascript:void(0);" onclick="foo(event)">Click
* *me</a>
Put something meaningful in the href attribute or replace the A
element with something more appropriate (a button or styled span
maybe).
The onclick listener should return false if the link should not be
followed:

<a href="usefulLink.html" onclick="return foo(event);"... </a>

and within foo:

function foo(e) {
if (...) {
/* all OK, don't follow the link */
return false;
} else {
...
}
}
The use of event as an argument to the call to foo means that both in
common event models a reference to the event object will be passed to
foo. It is the only way to get a reference to the event object from
an inline listener in browsers that don't implement window.event.

In any case, as suggested by Thomas, the simplest solution is to have
the listener pass a reference to the element using the this keyword:

<a ... onclick="return foo(this);"... </a>

and:

function foo(el) {
/* el is a reference to the node that called foo */
}

So el will be a reference to the element that has the listener,
regardless of the source or target element.
--
Rob
Sep 2 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mark | last post by:
I have a working PHP/MySQL application used for data entry. The data entry screen includes a "Save" button. The PHP code for this button looks like this: if (isset($_POST)) { if ($_POST ==...
5
by: Quinn | last post by:
When users clicked a unkown mime type link such as Zip on my website, a "Save/Open/Cancel" dialog box pops up. Is there a way to detect which button users clicked by using ASP? actually I only what...
6
by: chandramohan.mani | last post by:
I have table with 'n' number of rows. I want to highlight the row when ever i click on a particular row of the table. Can anyone please help me in this. Iam not using any Id's for the row. ...
7
by: Shaldaman | last post by:
Hi Is there a property in MS Access for the following: 1) For a Command Button on a form, is there a property that can be used to determine if it has been clicked? eg: Me!button7.Clicked - I...
7
by: Amadelle | last post by:
Hi all and thanks in advance, I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if...
1
by: Ben Amada | last post by:
Hi, I'm using the built-in calendar control. One problem I'm having is that if the user clicks on the day in the calendar which is already the SelectedDate, a postback occurs, but the...
1
by: puneet.bansal | last post by:
Hi, I want to know the index of the option that a user clicks on in a multiple-select object (regardless of whether he selected or deselected it). This seems fairly simple but I can't seem to...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
3
by: rn5a | last post by:
Consider the following code which creates LinkButtons dynamically: For i = 1 to 5 lnkBut = New LinkButton lnkBut.ID = "lnkBut" & i lnkBut.Text = i.ToString & " " lnkBut.CommandName = i...
4
by: ncsthbell | last post by:
I have a scenario that I have several buttons on a form, let's say, button1 & button2. When button1 is clicked, it runs button1 code. When button2 is clicked, it runs button2 code and then calls...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.