Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Simulating href click
Old 09-16-2008, 09:07 AM Simulating href click
Extreme Talker

Posts: 196
Trades: 0
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 -
Code:
window.addEvent('domready', function() {
     expandCollapse();
}

function expandCollapse(){
     $('expand').addEvent('click', function(){
          // do something
     }
}
html code -
Code:
<a id="expand" href="javascript:void(0);">open all</a>
i have tried this but id didnt work -
Code:
document.getElementById('expand').click();
What is the code to make the window.addEvent() fire? *expand* must be part of it. Thank you.
empiresolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-16-2008, 09:49 AM Re: Simulating href click
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Embarrassingly enough, I have no idea how to do this with regular JavaScript. It is easy to do if you learn the jQuery library, however:
Code:
$("#expand").trigger("click");
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-16-2008, 10:02 AM Re: Simulating href click
Extreme Talker

Posts: 196
Trades: 0
i use mootools extensively on this site and JQ and MT dont jive together. thanks though.
empiresolutions is offline
Reply With Quote
View Public Profile
 
Old 09-16-2008, 10:10 AM Re: Simulating href click
Extreme Talker

Posts: 196
Trades: 0
Thanks to Fang -

Working Solution -
Code:
function fireOnclick(objID) {
var target=document.getElementById(objID);
if(document.dispatchEvent) { // W3C
    var oEvent = document.createEvent( "MouseEvents" );
    oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
    target.dispatchEvent( oEvent );
    }
else if(document.fireEvent) { // IE
    target.fireEvent("onclick");
    }    
}
empiresolutions is offline
Reply With Quote
View Public Profile
 
Old 09-16-2008, 10:12 AM Re: Simulating href click
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Yes they do (jive together), all you do is noconflict the jQuery. Having two libraries together is pretty retarded, I must say, however, unless there is some type of extensive application, such as a site contained within a CMS, that makes two libraries more realistic.

There are a couple ways to do it, but the way I noconflict jQuery is like this:
Code:
(function($) {
//a bunch of code goes here, and you may use the $ object instead of jQuery
)(jQuery);
Of course, for such a small amount of code, you may just use jQuery instead of $, like this:
Code:
jQuery("#expand").trigger("click");
Of course, as I mentioned, two libraries for an average site is borderline retarded. Are you sure MooTools doesn't have a function for triggering clicks? I know YUI does, and most libraries should have some means of doing it, since it is a common debugging device.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-16-2008, 10:12 AM Re: Simulating href click
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Glad you solved it (looks like we were posting at the same time)...
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Simulating href click
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16457 seconds with 12 queries