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

Simulating href click

162 100+
i need to simulated having clicked on a link with JavaScript. As you will see in my stripped down example the resulting function from the click is a window.addEvent() action. Thanks for the help.

js code -
Expand|Select|Wrap|Line Numbers
  1. window.addEvent('domready', function() {
  2.      expandCollapse();
  3. }
  4.  
  5. function expandCollapse(){
  6.      $('expand').addEvent('click', function(){
  7.           // do something
  8.      }
  9. }
  10.  
html code -
Expand|Select|Wrap|Line Numbers
  1. <a id="expand" href="javascript:void(0);">open all</a>
i have tried this but id didnt work -
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('expand').click();
What is the code to make the window.addEvent() fire? *expand* must be part of it. Thank you.
Sep 16 '08 #1
2 3159
empiresolutions
162 100+
Thanks to Fang -

Working Solution -
Expand|Select|Wrap|Line Numbers
  1. function fireOnclick(objID) {
  2. var target=document.getElementById(objID);
  3. if(document.dispatchEvent) { // W3C
  4.     var oEvent = document.createEvent( "MouseEvents" );
  5.     oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
  6.     target.dispatchEvent( oEvent );
  7.     }
  8. else if(document.fireEvent) { // IE
  9.     target.fireEvent("onclick");
  10.     }    
  11. }
  12.  
Sep 16 '08 #2
rnd me
427 Expert 256MB
document.getElementById('expand').onclick()
Sep 16 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mike | last post by:
In my previous post, I wrote: > ... > GOAL: (very simple) Provide a hyperlink which, when clicked, > calls a javascript function which opens a new URL. > ... > PROBLEM: The following code...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
11
by: CW | last post by:
I have message entry screen that's causing me a bit of an issue. At the moment, there are 2 buttons, one is used to send message to another user (btnSend) and another is used to send messages to...
3
by: Brian Bischof | last post by:
I'm have a form with a datagrid showing a checkbox for each row. I want the user to be able to click a checkbox and then click a button that sends an email similar to how the HREF MailTo: works. I...
4
by: Roger Withnell | last post by:
I would like to freeze column and row headings on a webpage, simulating freeze panes as in an Excel spreadsheet. Don't seem to be able to do it with Frames. Is there a way with Javascript...
7
by: daveyand | last post by:
Is there a way in javascript to trigger the clicking of the left mouse button. Basically i have a floating div that is translucent (40% opacity) this div covers the whole screen. when a user...
7
by: maaravi | last post by:
Hi, I'm trying to simulate clicking on a link, but Firefox seems to disable it. In the sample below, when clicking on the div tag, the onclick event of the link is triggered, but the browser...
5
by: Xu, Qian | last post by:
Hello All, I have some problem by simulating a link click using javascript. The webpage uses a js-library named interface (jQuery like) ------------------------------ <a id="foo" href="#">Try...
1
by: paragbhavsar | last post by:
Hello Friends, Can we simulate click event of Input File Control through Javascript - I want this for Firefox 3.5 browser ? I have also tried with following code. Please let me know solution...
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
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:
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
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,...
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.