On May 21, 2011, at 12:38 PM, Mohnish J. wrote:

Hi Walter,

Can you please elaborate on how exactly I would be able to use
Event.preventDefault() with a conditional..?. Also why would I need to
use a conditional..? I ask this as I am a newbie to Jquery and my
javascript fundas are not very good.

To make it easier for you to answer this for me.. I have included the
part of the code in the partial where you say it would be appropriate to
put the button..

<%if @current_user.is_an_existing_member_of_group(@investor_group)%>
<%form_for :group_post, @group_post, :url => {:action => :post_message,
:id => params[:id]},:html => {:multipart => true}  do |f| %>
   Start Discussion:<%=f.text_field :message%>
  <%=  f.file_field :avatar %>
  <%=f.submit "Post" %>
 <%end%>

<div id = "latest_post"> </div>
<%for a in @group_all_posts %>
 <%if !a.avatar_file_name.to_s.empty? %>
 <br/> <%= image_tag a.avatar.url(:thumb) %>
 <%end%>
<br/><%= a.message %> <br/> by <%= Investor.find(a.post_by).first_name
%> <div class ="contentdispgrp" id="style_chck"> <%=
distance_of_time_in_words(a.created_at,Time.now) %> ago </div><br/ ><br/>
<hr/>
<%end%>


I would truly appreciate you extra effort in trying to help me..

Kindly bear with my being a novice.

Many Thanks for your inputs..

I'm looking at this code, and it appears to be a normal post to the server. Where and what do you plan to update to the page using jQuery? Are you saying you want to post to the server, but through JavaScript, and update the current page with a refresh of the list of messages? Because there's a well-worn way to do that. You stop the form.submit method, and use Ajax to send the form instead, then your server replies with the current list of messages, and you update the appropriate list DIV with those.

I don't know the jQuery idiom for this, but in Prototype, it's very simple:

$('your_form_id').observe('submit',function(evt){
        evt.stop(); //keep the default submit from happening
        this.request({ //send the form through an Ajax request
                onSuccess: function(transport){
                        $('your_list_container').update(transport.responseText);
                }
        });
});

As to the conditional, it doesn't apply in this pattern. But let's say you had a form submit button, and you wanted it to stop working after the form had been submitted (sort of what :disable_with does in the regular form builder code). You want the button to be pushed once only, so you might do something like:

$('your_button').observe('click',function(evt){
        if ( this.clicked ){
                evt.stop();
        }else{
                this['clicked'] = true;
        }
});

So the first time through, the button click happens and bubbles (and the button is marked so no future clicks will work) and the second + times it is clicked, it does nothing, because Event.stop happens instead. (Event.stop is a belt-and-suspenders form of Event.preventDefault, which is built in to most browsers, and I suspect added by jQuery to those that don't have it. It just makes the current event die and never propagate beyond the current element.

Walter


Walter Davis wrote in post #1000079:
On May 21, 2011, at 9:27 AM, Mohnish J. wrote:

How can i display the same onclick of a button without
making/getting the entire page to reload..

With Event.preventDefault(). If in Prototype, with Event.stop(). Be
sure to wrap this method in a conditional, otherwise you have just
killed the button and it will never bubble to submit the form again.

Walter

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails- t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en .


--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to