How to call javascript from a view

"
<%= javascript_include_tag :defaults %>

<% form_tag ‘/chap_two/index’ do %>

<%= text_field_tag :address1, params[:address1] %> <%= text_field_tag :address2, params[:address2] %> <%= submit_tag 'Save' %>
<% 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();” %>

On 9 Apr 2008, at 09:06, Adi wrote:

<%= text_field_tag :address2, params[:address2] %>
<%= submit_tag 'Save' %>
<% end -%>

I’m trying to execute a javascript function when i click the “save”
button…

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

On 9 Apr 2008, at 10:10, Adi wrote:

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

thanks for replying…
it’s “half” works…
i say it because
i only see an alert but the second command is not working…

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

Is there any reason why it happen??

Thanks,

Adi

On 9 Apr 2008, at 10:26, Adi wrote:

well, it’s a container that contains kinda google map… it will show
the driving direction (best one from google’s version) from one
address to another address…

about the debugger, well honestly i don’t even know it…

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

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] %>

<%= submit_tag ‘Show’ %>

<% 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] %>

<%= submit_tag ‘Show’ %>

<% end -%>
"

On 11 Apr 2008, at 03:41, Adi wrote:

"
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