473,748 Members | 4,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programatically click <INPUT type=image ...> element

I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE
5.5+). This should work regardless of whether IE has focus. Normally you would do myDomElement.cl ick and the mouse
doesn't matter, but in the case of an input image element, what happens is the submitted url has something like
"?x=12&y=7" appended to it (the numbers vary per mouse position on the clicked element). If you hit the spacebar
while that element has focus or do the .click path, then you wind up with "?x=0&y=0" appended to the submitted url,
which is no good for me.
One possible idea that I have not gotten to work is to create an approriately named hidden element (in our case
<input type=hidden name=x> and <input type=hidden name=y>) and set up an onClick handler and insert the position
values that way [document.all.x. value = myImageElement. offsetWidth / 2]. However, if I insert this element at the
end of the form, what happens then is the submitted url's string has a string like "?x=14&x=0& y=0" appended, which is
not good enough (for example, PHP will eat the x=14, and who's to say what other web servers do?) The situation is
even worse if I insert the hidden element at the beginning of the form, because then the initial click handler will
set the hidden element's value, but then quietly dies. Perhaps this method can be patched up, though.

The other idea that I had was to use .click with .fireEvent as the code below illustrates (it can also be used to
test the above hidden element idea). However, while I am able to modify the click event, the onSubmit event's values
don't change. In particular, if I position the mouse above the image element before pressing the "Click me" button,
then the mouse coordinates for the onSubmit event appear correct, but the final string comes out with 0's. Similar
comments apply if you press the space bar while the focus is on the image element.

Thanks,
Csaba Gabor from New York

<HTML>
<HEAD>
<TITLE>fireEven t testing</TITLE>
<SCRIPT>
var TmpOnClick; // a unique temporary variable
function clickHandler() {
// clickHandler is supposed to rewrite the x,y coordinates of the mouse click
var myButton = document.getEle mentsByTagName( "INPUT")[0];
var newEvent = document.create EventObject();

newEvent.offset X = myButton.offset Width / 2; // middle of image
newEvent.offset Y = myButton.offset Height / 2; // middle of image
newEvent.cancel Bubble = false;
newEvent.button = 1;
myButton.onclic k = window.TmpOnCli ck; // Replace the original click handler
myButton.fireEv ent ("onclick", newEvent); // Allow the original handler to take over
event.cancelBub ble = true; // Cancel the on hold original event
}

function clickMeSub() {
var myButton = document.getEle mentsByTagName( "INPUT")[0];
window.TmpOnCli ck = myButton.onclic k; // Save any old click handler
myButton.onclic k = clickHandler // Insert our click handler
myButton.click( ); // Now invoke it
}
</SCRIPT>
</HEAD>
<BODY bgcolor=yellow style=margin-left:.4in>
<FORM onsubmit="alert ('submit fired: '+event.offsetX +','+event.offs etY);return true;">
<INPUT type=image
onclick="alert( 'on click fired: '+event.offsetX +','+event.offs etY);return true;"
title="Click with mouse to see both onClick and onSubmit fire."
accesskey=s value=SubmitMe>
&nbsp;
<BUTTON onclick="clickM eSub();return false;"
title="Simulate s a click event, but fails to submit mouse coordinates."
accesskey=c><u> C</u>lick me</BUTTON>
</FORM>
</BODY>
</HTML>
Jul 20 '05 #1
4 17256
Csaba2000 wrote:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE
5.5+). This should work regardless of whether IE has focus. Normally you would do myDomElement.cl ick and the mouse
doesn't matter, but in the case of an input image element, what happens is the submitted url has something like
"?x=12&y=7" appended to it (the numbers vary per mouse position on the clicked element). If you hit the spacebar
while that element has focus or do the .click path, then you wind up with "?x=0&y=0" appended to the submitted url,
which is no good for me.

<snip>
I gather program supplied x and y values need to be submitted if
submission is by program or keyboard operation.

One solution is to have the image input click handler discriminate
between mouse click and keyboard event by means of an onmouseup handler.
For mouse click, submission is allowed to proceed as normal with the
browser appending mouse coordinates. Following a keyboard event,
however, submission is cancelled and a timer used to call programmatic
submission using form.submit functionality.

Using one of your ideas, the programmatic submit function dynamically
adds hidden form fields named "x" and "y" with appropriate values before
calling form.submit, avoiding duplicate entries for x and y values in
the process. Appears to check out in IE5, 6, NS6 and Mozilla.

Submitting in the background (without focus) could prove difficult.
Personally I leave confirm dialogs enabled for form submission and would
definitely cancel a request generated whilst away from a browser window
- but that's me.

HTH
Dom

======== test code =========

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitl ed strict.dtd</title>
<script type="text/javascript">

var imgClicked=fals e; // set by mouseup

function imageSubmit()
{
if(imgClicked)
{ imgClicked = false; // in case of later cancellation
return true;
}
window.setTimeo ut("progSubmit( )",50); // don't submit on this event
return false;
}
function progSubmit()
{
var form = document.forms. testForm;
alert("progSubm it()");
var el = document.create Element("INPUT" );
el.type="hidden ";
el.name="x";
el.value="5"; // whatever
form.appendChil d(el);
el = document.create Element("INPUT" );
el.type="hidden ";
el.name="y";
el.value="8"; // idem
form.appendChil d(el);
form.submit();
return true;
}

</script>
</head>
<body>
<form name="testForm" id="testForm" action="test.ht ml">
<input type="image" src="whatever.p ng" id=myImg
onmouseup="imgC licked=true";
onclick="return imageSubmit()">
</form>
</body>
</html>


Jul 20 '05 #2
On 04 Aug 2003 23:14:03 GMT, "Csaba2000" <ne**@CsabaGabo r.com> wrote:
I want to be able to programatically click on the center of an
<INPUT type=image ...> element (I only care about IE
5.5+). This should work regardless of whether IE has focus.


Do you really?

Are you sure you don't want to perform an http request to the server
such as foo.x=100&foo.y =200 if so why not ask how to do that? it's a
lot easier.

What reason do you actually want to perform the bizarre request above?
As always ask a question about your real problem...

Jim.
--
comp.lang.javas cript FAQ - http://jibbering.com/faq/

Jul 20 '05 #3
Dom,

thank you, Thank You, THANK YOU! I am amazed and stunned,
but your example is working for me, too. At first I thought
it was because you are creating the x and y elements with
javascript, but the point is that your method works because
of the form.submit. The image element won't submit itself
unless its being clicked. Very nifty! Great thinking!
As I mentioned to the other respondant, it was only yesterday
that I saw your post when I was looking for my original writeup
on Google. Neither of the two replies to my original post show
up in my Outlook Newsreader. Go figure.
A comment on my adaptation of your solution. This is just
me thinking out loud. Any presubmission handling should happen
before I (my IE exerciser) get involved. So it means that I should
hook myself into the final handler, and note the return value.
Cancel it in any case. However, if we returned true, that's
when I create the x,y elements and do the form.submit. Hmmm,
I'll have a little bit of reading to do to figure out how
to make sure I'm the last guy in the chain, but this looks
workable and I don't introduce artifacts if the submission is
cancelled. This should work. Cool. Majorly cool.

I'd like to clarify your last remark, in case you see this reply.
It was unclear to me whether you talking from the point of view of
the client or the server. If your comment about the alert boxes
is from the client's (user's) point of view: The user is expected
to know the sequence of actions they are directing IE to do.
In particular, they should expect the confirm/alert boxes. These
can be detected, responded to, and dismissed (It's one of the
directives that my IE exerciser (should - it's untested) allows.
From the server's point of view, there should be absolutely zilch
detectable difference. If there is, the simulation is not true.
Hopefully, I've understood your point.
Thanks again Dom,
Csaba Gabor from New York

Dom Leonard <do************ *@senet.andthis .com.au> wrote in message news:<XO******* ********@nnrp1. ozemail.com.au> ...
Csaba2000 wrote:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE
5.5+). This should work regardless of whether IE has focus. Normally you would do myDomElement.cl ick and the mouse
doesn't matter, but in the case of an input image element, what happens is the submitted url has something like
"?x=12&y=7" appended to it (the numbers vary per mouse position on the clicked element). If you hit the spacebar
while that element has focus or do the .click path, then you wind up with "?x=0&y=0" appended to the submitted url,
which is no good for me.

<snip>
I gather program supplied x and y values need to be submitted if
submission is by program or keyboard operation.

One solution is to have the image input click handler discriminate
between mouse click and keyboard event by means of an onmouseup handler.
For mouse click, submission is allowed to proceed as normal with the
browser appending mouse coordinates. Following a keyboard event,
however, submission is cancelled and a timer used to call programmatic
submission using form.submit functionality.

Using one of your ideas, the programmatic submit function dynamically
adds hidden form fields named "x" and "y" with appropriate values before
calling form.submit, avoiding duplicate entries for x and y values in
the process. Appears to check out in IE5, 6, NS6 and Mozilla.

Submitting in the background (without focus) could prove difficult.
Personally I leave confirm dialogs enabled for form submission and would
definitely cancel a request generated whilst away from a browser window
- but that's me.

HTH
Dom

======== test code =========

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitl ed strict.dtd</title>
<script type="text/javascript">

var imgClicked=fals e; // set by mouseup

function imageSubmit()
{
if(imgClicked)
{ imgClicked = false; // in case of later cancellation
return true;
}
window.setTimeo ut("progSubmit( )",50); // don't submit on this event
return false;
}
function progSubmit()
{
var form = document.forms. testForm;
alert("progSubm it()");
var el = document.create Element("INPUT" );
el.type="hidden ";
el.name="x";
el.value="5"; // whatever
form.appendChil d(el);
el = document.create Element("INPUT" );
el.type="hidden ";
el.name="y";
el.value="8"; // idem
form.appendChil d(el);
form.submit();
return true;
}

</script>
</head>
<body>
<form name="testForm" id="testForm" action="test.ht ml">
<input type="image" src="whatever.p ng" id=myImg
onmouseup="imgC licked=true";
onclick="return imageSubmit()">
</form>
</body>
</html>
Jul 20 '05 #4
May be you should try to call submit() on your form.

Say you form id is "theform",
so in the end of your click handler
you write document.forms["theform"].submit();

"Csaba2000" <ne**@CsabaGabo r.com> wrote in message
news:bg******** @dispatch.conce ntric.net...
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would do myDomElement.cl ick and the mouse doesn't matter, but in the case of an input image element, what happens is the submitted url has something like "?x=12&y=7" appended to it (the numbers vary per mouse position on the clicked element). If you hit the spacebar while that element has focus or do the .click path, then you wind up with "?x=0&y=0" appended to the submitted url, which is no good for me.
One possible idea that I have not gotten to work is to create an approriately named hidden element (in our case <input type=hidden name=x> and <input type=hidden name=y>) and set up an onClick handler and insert the position values that way [document.all.x. value = myImageElement. offsetWidth / 2]. However, if I insert this element at the end of the form, what happens then is the submitted url's string has a string like "?x=14&x=0& y=0" appended, which is not good enough (for example, PHP will eat the x=14, and who's to say what other web servers do?) The situation is even worse if I insert the hidden element at the beginning of the form, because then the initial click handler will set the hidden element's value, but then quietly dies. Perhaps this method can be patched up, though.
The other idea that I had was to use .click with .fireEvent as the code below illustrates (it can also be used to test the above hidden element idea). However, while I am able to modify the click event, the onSubmit event's values don't change. In particular, if I position the mouse above the image element before pressing the "Click me" button, then the mouse coordinates for the onSubmit event appear correct, but the final string comes out with 0's. Similar comments apply if you press the space bar while the focus is on the image element.
Thanks,
Csaba Gabor from New York

<HTML>
<HEAD>
<TITLE>fireEven t testing</TITLE>
<SCRIPT>
var TmpOnClick; // a unique temporary variable
function clickHandler() {
// clickHandler is supposed to rewrite the x,y coordinates of the mouse click var myButton = document.getEle mentsByTagName( "INPUT")[0];
var newEvent = document.create EventObject();

newEvent.offset X = myButton.offset Width / 2; // middle of image
newEvent.offset Y = myButton.offset Height / 2; // middle of image
newEvent.cancel Bubble = false;
newEvent.button = 1;
myButton.onclic k = window.TmpOnCli ck; // Replace the original click handler myButton.fireEv ent ("onclick", newEvent); // Allow the original handler to take over event.cancelBub ble = true; // Cancel the on hold original event }

function clickMeSub() {
var myButton = document.getEle mentsByTagName( "INPUT")[0];
window.TmpOnCli ck = myButton.onclic k; // Save any old click handler
myButton.onclic k = clickHandler // Insert our click handler
myButton.click( ); // Now invoke it
}
</SCRIPT>
</HEAD>
<BODY bgcolor=yellow style=margin-left:.4in>
<FORM onsubmit="alert ('submit fired: '+event.offsetX +','+event.offs etY);return true;"> <INPUT type=image
onclick="alert( 'on click fired: '+event.offsetX +','+event.offs etY);return true;" title="Click with mouse to see both onClick and onSubmit fire."
accesskey=s value=SubmitMe>
&nbsp;
<BUTTON onclick="clickM eSub();return false;"
title="Simulate s a click event, but fails to submit mouse coordinates."
accesskey=c><u> C</u>lick me</BUTTON>
</FORM>
</BODY>
</HTML>

Jul 20 '05 #5

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

Similar topics

4
21296
by: Martin Lucas-Smith | last post by:
I am wanting to know whether are XHTML1-valid characters for use within an id attribute and/or a name attribute. http://validator.w3.org/check?uri=http://www-thaihydro.geog.cam.ac.uk/riverslice/&ss=1&verbose=1 (e.g. see line 77) suggests that it is valid, but the spec suggests that it is not. More detail:
3
12196
by: iinet | last post by:
How can i set in my css a min width for input elements, but leave the max size dynamic? Ben
3
13154
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that wraps beneath it I'd prefer it looked like this, with a flush left margin:
5
2257
by: Bart van Deenen | last post by:
Hi all I have a form with a couple of input fields, embedded within spans. I am using script.aculo.us for dragging and dropping, and want to reorder the input fields that way. The input fields are display:inline because I want them all on the same line. Does anyone know of a smart trick to be able to drag these input fields? Just setting their disabled attribute doesn't work, because then they get no events. Not setting disabled just...
2
5328
by: Mara Guida | last post by:
Hi, After I've populated a page with some <INPUT ...> elements I'd like to have access to whatever is written there. The best I've done (which does not work) is: ================ // ==UserScript== // @name AddInput
4
18434
by: Rick | last post by:
I'm trying to make an input tag visible or hidden based on the value of another input tag, so that when the 1st input tag is changed in anyway, the 2nd input tag will become visible. Thanks in advance, Rick
2
3287
by: jp2code | last post by:
I have several input fields on my form, and the form works; however, Visual Studio is showing errors, and I would like to get rid of them. When the form is submitted, it is redirected back to itself, where it checks the values that were entered. If the default values are set, this is the visitor's first time to access the page. Example: The FirstName field would initially be displayed with "First Name;"
1
2537
by: JPhilS | last post by:
Hi to all Webmaster! I have discover this problem when I inserted the scores of the students i a centain subject. I am making a school project with regards to saving students' record. first, I have to view all students under the certain subject and at the last field, I putted a form to put the scores of each student. say '<input type=text name='score' size=10>' and number of '<input type=text>' is equal to the number of students. I...
1
5506
by: test9991014 | last post by:
Hi folks, I've got something like this: <table> <tr> <td>1</td> <td align=center> <input type=text> </td>
8
3626
by: Zhang Weiwu | last post by:
hello. Is it possible to design CSS in the way that content in <inputare not visible in print out (a.k.a. value of <inputnot visible) while the border remain visible? trial: input { border: thin solid black;
0
8991
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
9544
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
9372
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
9247
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...
0
8243
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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
6074
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();...
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.