473,395 Members | 2,010 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,395 software developers and data experts.

Identifying the element clicked

Hello,

Here is my problem: I have a site that uses urls of this format to
submit forms:

<a id="1_1_2_0"
href="javascript:if(validate)submitForm(...)">Subm it</a>
I'm just a third-party programmer, I have no control over this
format. I am tasked with writing a function that will be able to fire
on the click and within that function I need to identify exactly which
submission was fired. The function will run inside the validation
code.

When called within the href, 'this' seems to refer to the entire
window, not to the anchor element. For example:

<script>

function list(obj){
var text = "";
for (n in obj){
text += n + "\n";
}
alert(text);
}

</script>
<a id="1_1_2_0"
href="javascript:if(validate)list(this);submitForm (...)">Submit</a>

Thus, I can't use the ordinary method of 'this.id' which would return
the link id value.

Is there some way within that href of dynamically determining which
anchor is being clicked? I can't see it.

thanks.

mp

--
cat: $HOME/.signature: No such file or directory
Feb 14 '08 #1
3 2309
On 14 ćĹ×, 12:02, Michael Powe <michael+g...@trollope.orgwrote:
Hello,

Here is my problem: I have a site that uses urls of this format to
submit forms:

<a id="1_1_2_0"
href="javascript:if(validate)submitForm(...)">Subm it</a>

I'm just a third-party programmer, I have no control over this
format. I am tasked with writing a function that will be able to fire
on the click and within that function I need to identify exactly which
submission was fired. The function will run inside the validation
code.

When called within the href, 'this' seems to refer to the entire
window, not to the anchor element. For example:

<script>

function list(obj){
var text = "";
for (n in obj){
text += n + "\n";
}
alert(text);

}

</script>
<a id="1_1_2_0"
href="javascript:if(validate)list(this);submitForm (...)">Submit</a>

Thus, I can't use the ordinary method of 'this.id' which would return
the link id value.

Is there some way within that href of dynamically determining which
anchor is being clicked? I can't see it.

thanks.

mp

--
cat: $HOME/.signature: No such file or directory
Hi. See this.
function list(aEvt){
var element = typeof aEvt.which=="undefined" ?
aEvt.srcElement : aEvt.target;
alert(element.href);
return false;
}

<a href="tes.html" onclick="return list(event);">test</a>
Feb 14 '08 #2
On Feb 14, 10:27 am, Georgi Naumov wrote:
<snip>
function list(aEvt){
var element = typeof aEvt.which=="undefined" ?
aEvt.srcElement : aEvt.target;
Why the unfounded inference from the type of a - which - property of
an event object to the use of its - srcElement - or - target -
properties? Surly the trueness of the values of the - srcElement - and/
or - target - properties would have a reliable and direct relationship
with the viability of employing them?

alert(element.href);
return false;
}

<a href="tes.html" onclick="return list(event);">test</a>
In the - onclick - attribute code of an A element the - this - keyword
can reliably be expected to refer to the element, making your - list -
function needlessly complex in context. The OP's problem is that he is
attempting to use the HREF attribute to execute code and code from
that source is executed in the global context and there the - this -
keyword refers to the window/global object. The solution to that
problem is to abandon the use of the HREF to execute code and instead
use an - onclick - attribute, especially as, to the best of my
recollection of that distant a past, the only browsers where executing
code in an HREF was supported for A elements and an - onclick -
handler was not were Netscape <=2, and nobody worries about those
browsers any more.
Feb 14 '08 #3
Michael Powe wrote:
Here is my problem: I have a site that uses urls of this format to
submit forms:

<a id="1_1_2_0"
href="javascript:if(validate)submitForm(...)">Subm it</a>
I'm just a third-party programmer, I have no control over this
format.
You would be well-advised to lose the bad framework that generates this
inaccessible code element then. Proper validating and submitting forms
is done as follows:

<form ... onsubmit="return validate;">
...
<input type="submit" ...>
or
<input type="image" ...>
</form>

Usually `validate' would be `validate()', calling a validation function that
returns `true' on success if scripting is supported, submitting the form,
`false' otherwise. If there was no scripting support, the `onsubmit'
attribute would be ignored; that also emphasizes that you have to do
server-side validation anyway.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Feb 17 '08 #4

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

Similar topics

4
by: Armando | last post by:
Hey again - I have a bunch of similar controls on a form (A2000), and I'm calling the same code (with an argument as to what was clicked) when any one of them is clicked. I'd like to generalize...
4
by: pamelafluente | last post by:
Hello. A question. It is possible to refer to the ID of an element within the element itself? And, if yes, what is the instruction or function. Example. Assume I have this: <div id="Square1" ...
1
by: daldridge | last post by:
I have a unique-elements/sorting question (who doesn't?), but haven't yet been able to get appropriate template/select/for-each processing working. I don't fully grok the Muenchian technique yet...
6
by: Vortexmind | last post by:
Hi all I was wondering if this is possible in Javascript. I want to make a bookmarklet. When a user launches it, it tells the user to select an element in the page (for example a textarea) with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
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,...

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.