Search code examples
javascriptjqueryajaxprototypejs

Simulating click and change with Prototype - Simulate.js


I have the following Prototype code acting as an observer and I'm trying to trigger off the observer, after setting the value of the select manually...

select.observe('change', this.onChange.bindAsEventListener(this));

I was originally trying to fire this off in a number of ways via jQuery, until I realized it wouldn't work. I then adopted the script, 'event.simulate.js' as outlined in this answer. The problem I'm now experiencing is that the console shows the following error...:

Uncaught TypeError: Cannot call method 'simulate' of null scripts.js?v=56:701

I thought my code was correct...:

$('div.amfinder-horizontal td:nth-child(1) select').simulate('click');

The webpage (if it's helpful) can be seen here. I hope it's an easy fix, and would appreciate any and all guidance even if it is a suggestion on how to handle this differently.

Thanks!


Solution

  • The $() method only selects via id so this statement

    $('div.amfinder-horizontal td:nth-child(1) select').simulate('click');
    

    is invalid - you can however do this

    $$('div.amfinder-horizontal td:nth-child(1) select').first().simulate('click');
    

    $$() is the CSS selector method which returns an array of elements that match the selector. then use first() or [0] to identify the first element (or only) in the array which you can then run the method simulate()