how to call javascript from a view

" <%= javascript_include_tag :defaults %>

</head>   <body>

<% form_tag '/chap_two/index' do %>   <div>     <%= text_field_tag :address1, params[:address1] %>     <%= text_field_tag :address2, params[:address2] %>     <%= submit_tag 'Save' %>   </div> <% end -%> "

I'm trying to execute a javascript function when i click the "save" button... can anyone tell me how...

Try with

<%= submit_tag 'Save', :onclick => "javascript_method();" %>

You could stick an onclick on the submit button. However, I suspect that you actually want an onsubmit on the form, ie form_tag('chap_two/index', :onsubmit => '...'). This will trigger if the user submits the form by hitting the enter key, whereas an onclick on the submit button obviously wouldn't

Fred

form_tag('chap_two/index', :onsubmit => '...') would be better.

at last... it "half" works... i can call the function and it shows an alert but the second command is not working... i assume it because the gdir is not loaded...

    function setDirections(fromAddress, toAddress, locale) {     alert("a");       gdir.load("from: " + fromAddress + " to: " + toAddress,                 { "locale": locale });     }

is there anything i can do with this...???

Thanks,

Adi

at last... it "half" works... i can call the function and it shows an alert but the second command is not working... i assume it because the gdir is not loaded...

   function setDirections(fromAddress, toAddress, locale) {     alert("a");      gdir.load("from: " + fromAddress + " to: " + toAddress,                { "locale": locale });    }

I don't have the faintest clue what gdir is, however if you're not
already using firebug to debug your javascript code, now is the time
to start

Fred

If you don't, then it's time to learn. Install the firebug extension for firefox and you'll have a pretty handy javascript debugger (and dom inspector, css viewer etc...)

Fred

Great, it works... i've put these code below "   <% form_tag "/chap_two/find", :onsubmit => "setDirections('san fransisco', 'new york','en_US')" do -%>     <%= text_field_tag :address1, params[:address1] %>     <%= text_field_tag :address2, params[:address2] %>     <div><%= submit_tag 'Show' %></div>   <% end -%> " but what if i want to use my params[:address1] and params[:address2] instead of 'san fransisco' and 'new york'... i've tried these code below but it's not working...

"   <% form_tag "/chap_two/find", :onsubmit => "setDirections("&<% params[:address1] %> & ", " & <% params[:address1] %> & ",'en_US')" do -%>     <%= text_field_tag :address1, params[:address1] %>     <%= text_field_tag :address2, params[:address2] %>     <div><%= submit_tag 'Show' %></div>   <% end -%> "

I have this function below inside my public/application.js and i'm wondering whether i can replace "San Fransisco" and "New York" in this command "setDirections("San Francisco", "New York", "en_US");" with session[:address1] and session[:address2]....??? Please can anyone show me how to do it... "     function initialize() {       if (GBrowserIsCompatible()) {                 alert("kyaaaaaaaa")

        map = new GMap2(document.getElementById("map_canvas"));         gdir = new GDirections(map, document.getElementById("directions"));         GEvent.addListener(gdir, "load", onGDirectionsLoad);         GEvent.addListener(gdir, "error", handleErrors);         setDirections("San Francisco", "New York", "en_US");       }     } " "

Great, it works... i've put these code below "   <% form_tag "/chap_two/find", :onsubmit => "setDirections('san fransisco', 'new york','en_US')" do -%>     <%= text_field_tag :address1, params[:address1] %>     <%= text_field_tag :address2, params[:address2] %>     <div><%= submit_tag 'Show' %></div>   <% end -%> " but what if i want to use my params[:address1] and params[:address2] instead of 'san fransisco' and 'new york'... i've tried these code below but it's not working...

setDirections($F('address1'), $F('address2'), 'en_US') should do the
trick, assuming that those text fields have their id set to address1
and address2. Also (unless you want the form submitted as well as your
js called) you probably want a return false; at the end of your
onsubmit.

Fred