title: Handling javascript onchange events with Mechanize rubygem
body: <p>I have been trying to write a script that fetches results from my university website.  Someone suggested that I use Mechanize and it does look really promising.</p>
  
  <p>In order to get the result, one has to first enter the roll number and then select the session.
  Simulating the first part has been easy with Mechanize, but with the second part I'm having problems as it is actually a JavaScript <code>onchange</code> event.</p>
  
  <p>I read the function definition in the JavaScript and this is what I have come up with so far.  Mechanize can't handle the onchange event and also when I pass the values that are actually changed by the JavaScript function manually, the same page is returned.</p>
  
  <p>Here's the javaScript Code</p>
  
  <pre><code>function __doPostBack(eventTarget, eventArgument) {
          var theform;
          if (window.navigator.appName.toLowerCase().indexOf("microsoft") &gt; -1) {
              theform = document.Form1;
          }
          else {
              theform = document.forms["Form1"];
          }
          theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
          theform.__EVENTARGUMENT.value = eventArgument;
          theform.submit();
      }
  </code></pre>
  
  <p>I set a breakpoint in firebug and found the value of <code>__EVENTTARGET</code> to be 'Dt1', whereas <code>__EVENTARGUMENT</code> stays ''.</p>
  
  <p>The ruby script that I have written to do this is</p>
  
  <pre><code>require 'mechanize'
  
  #set up the agent to mimic firefox on windows
  agent = Mechanize.new
  agent.keep_alive = true
  agent.user_agent = 'Windows Mozilla'
  
  page = agent.get('http://www.nitt.edu/prm/nitreg/ShowRes.aspx')
  
  #using mechanize to get us past the first form presented
  result_form = page.form('Form1')
  result_form.TextBox1 = '205110018'
  
  page = agent.submit( result_form, result_form.buttons.first )
  
  #the second hurdle that we encounter,
  #here i'm trying to get past the JavaScript by doing what it does manually
  result_form = page.form('Form1')
  result_form.field_with('Dt1').options.find { |opt| opt.value == '66' }.select
  result_form.field_with( :name =&gt; '__EVENTTARGET' ).value = 'Dt1'
  
  #here i should have got the page with the results
  page = agent.submit(result_form)
  pp page
  </code></pre>
  
  <p>Can anyone tell me what I'm doing wrong? </p>