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

Cross-browser alternative to click() event

Hi. I just ran into a situation where I want to emulate the IE
specific obj.click() syntax on an object on the webpage.

The most convenient thing for me is if I were able to just select my
object

document.getElementById('myobj').click()

Do you know any cross-browser solutions to that? (must work in firefox
as a minimum).

Thanks

/Jesper Rønn-Jensen

Jan 30 '07 #1
7 10005
This may not be the best answer but see if you can get something along
these lines to work.I used it for drag and drop.

js script:
function mouseOver(ev){
ev = ev || window.event;
var target = ev.target || ev.srcElement;
if(!target) return;

if(target.id == 'DRAG'){
target.onmousedown = function(ev){
dragObject = this;
mouseOffset = getMouseOffset(this, ev);
return false;
}
}
}

function mouseDown(ev){
ev = ev || window.event;
var target = ev.target || ev.srcElement;

//code you want to execute.
}

document.onmouseover = mouseOver;
document.onmousedown = mouseDown;
//end script

html code:
<html>
<script src="js"></script>
<body>
<div id='drag'></div>
</body>
</html>

Let me know if that helps you!

Jan 30 '07 #2
On Jan 30, 4:57 pm, "Jesper Rønn-Jensen" <jespe...@gmail.comwrote:
Hi. I just ran into a situation where I want to emulate the IE
specific obj.click() syntax on an object on the webpage.

The most convenient thing for me is if I were able to just select my
object

document.getElementById('myobj').click()

Do you know any cross-browser solutions to that? (must work in firefox
as a minimum).

Thanks

/Jesper Rønn-Jensen
click() is supported by FireFox.
http://developer.mozilla.org/en/docs/DOM:element.click
This example works well:

<div id="myobj" onclick="alert('Clicked!');">Click me</div>
<script>
document.getElementById('myobj').click();
</script>

Maybe you just need a cross-browser event binding method? Try Event
object from http://www.prototypejs.org.

Sincerely,
Alexander
http://www.alexatnet.com - PHP/Ajax notes, tips, tutorials
Jan 30 '07 #3
Adambrz, Alexander

Thanks for the tips. I'll look into it and see if that can help my
situation when I get back to work. I really appreciate your thoughts.
Click() in Firefox is new to me, so obviously there must be something
else bugging.

/Jesper

Feb 2 '07 #4
Alexander wrote:
click() is supported by FireFox. http://developer.mozilla.org/en/docs/DOM:element.click
Well, it turns out the page you're linking to explains that only
certain input elements will respond to click() in Gecko (firefox,
mozilla, etc.).

That explains why I can't get your example to work in firefox --
because the <divelement is not an input element.

According to the W3C DOM level 2 spec (that the link you provided
links to), this is how the browser should work:
click
Simulate a mouse-click. For INPUT elements whose type attribute
has one of the following values: "button", "checkbox", "radio",
"reset", or "submit".
(from http://www.w3.org/TR/DOM-Level-2-HTM...tml#ID-2651361)

Which means that IE is too generous in it's support according to the
spec.

Feb 2 '07 #5
VK
On Feb 3, 1:30 am, "Jesper Rønn-Jensen" <jespe...@gmail.comwrote:
Click() in Firefox is new to me, so obviously there must be something
else bugging.
click() is DOM 0 method (Netscape 2.0) so any normal browser - and any
browser pretending to be such - supports it in general. The problem is
the that applicability of this method was narrowed over the past years
as part of user interface protection. In the particular on different
browsers click() may be silently ignored for form upload control,
select control options and links. Overall currently you may relay on
click() only for form controls _except_ file upload control and select
control.
Custom events as suggested in other post do not change anything in the
security limitations: wherever click() is not allowed, custom events
will not work either.

The fact that you are happy with IE but asking about Firefox suggests
that your [object] is in fact hidden file upload control you are
triggering by click(). If I'm right then sorry but this bird won't
fly: on Gecko browsers it is a part of user interface protection.
Feb 2 '07 #6
VK
On Feb 3, 1:42 am, "Jesper Rønn-Jensen" <jespe...@gmail.comwrote:
Well, it turns out the page you're linking to explains that only
certain input elements will respond to click() in Gecko (firefox,
mozilla, etc.).

That explains why I can't get your example to work in firefox --
because the <divelement is not an input element.
Oh, so you need it for DIV? Sorry then to everyone, createEventObjec/
fireEvent (IE) + createEvent/dispatchEvent (Gecko) will work.
Be aware that artificial event are not propagated (do not "bubble
up"). It may be important or not for your case, but just to mention.

Feb 2 '07 #7
Mmm same problem here..
Here's my solution.
Note that this function would need more appropriate error handling
etc..
currently tested on firefox 2 and IE 6

---------------------8<----------------------------------

function doClick(obj) {
try {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,0, 0, 0, 0, 0,
false, false, false, false, 0, null);
var canceled = !obj.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
} else {
// None of the handlers called preventDefault
}
} catch(er) {
obj.click(); //IE
}
}

-------------------->8----------------------------------

just call this function like this:
doClick(document.getElementById(OBJECT_ID));

Check this out for more info:
http://developer.mozilla.org/en/docs....dispatchEvent
http://developer.mozilla.org/en/docs...initMouseEvent

Bye
Sebastian
www.sebastian.it

Feb 17 '07 #8

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

Similar topics

4
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count...
2
by: Sidorf | last post by:
Hi It's a little hard for me to explain my problem, but i'll try. I have an application in which i have a ManagerClass, many ClientClass-es ans some ControlClass. The ManagerClass creates one...
4
by: Bob | last post by:
Hi, Moving a project from .net 2003 -> 2005 Beta 2 Windows App. Main Window is start object. Main window spawns a thread. After doing some work this thread raises an interrupt. The event carries...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
11
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C#...
3
by: Pieter Coucke | last post by:
Hi, In my VB.NET 2005 application I'm generating and sending emails using the outlook-object model (2003). When a mail is Send (MailObject_Send), I raise an event in a global class, that is...
2
by: VK | last post by:
Cross-Domain Interactor (CDI) StarGates 0.9 is released I believe that the post "JSON - Cross domain request"...
10
by: Daniel | last post by:
Hi guys I have a form with my gui on, just some list boxes. And a class that handles incoming data. i want to, on receiving data, update my gui. However even though i have an instance...
4
by: Paul Cheetham | last post by:
Hi, I have a couple of classes that I am using to read a swipe-card reader attached to the serial port (c# VS 2005 Pro). I have a SerialComm class which actaully reads the serial port, and a...
6
by: AliR \(VC++ MVP\) | last post by:
Hi everyone, I have a Socket derived class that parses the received data into a message, populates a object with that information and then tries to send an event to inform the owner of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...

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.