Update a specific DIV without refreshing the page

Asked

Viewed 3,061 times

1

How to update a DIV without refreshing the entire page?

Formerly in Rails 2.3.10, remote_function was used to update a certain div.

Now in Rails 4.2, since you could no longer 'remote_function' how can I do this?

Ex: When typing an ID

'<%= text_field :individual_id, (something like :update => 'search') %>'

Person name of this ID will appear

div id='search'

'<%=h @molecular_bio.individual && @molecular_bio.individual.name %> '

/div'

I tried using this command in javascript, but I don’t know much about js.

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">

var live_search_timer = null;


function start_live_search(){

    clearTimeout(live_search_timer);
    

live_search_timer = setTimeout(function(){

$('#search').fadeOut('slow').(comando para dar um update na div).fadeIn('slow'); }, 2000); }

</script>

<%= text_field :individual_id, :onKeyPress => "start_live_search()" %>

  • Have you tried using ajax ( Jquery or Javascript ) to make this update?

  • Yes, I tried. I will edit the question with the javascript code I tried

  • Hi Michael, I just answered a question with related content using Jquery and Ajax, I hope to help you, this is the link: http://answall.com/questions/144104/executar-php-sem-update-toda-p%C3%A1gina

3 answers

1

Create a JS function

function updateDiv(div) {
    var div = '#' + div;
    $(div).load(window.location.href + " " + div);
}

or goes straight through the console

 $('#div').load(window.location.href + " " + '#div');


<div id="teste">

0

using unobtrusive javascript in Rails links

#view
<% form_tag @molecular_bio, url:edit_molecular_path(@molecular_bio) :method => :get, :remote => true do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

<div id='search'>

<%=h @molecular_bio.individual && @molecular_bio.individual.name %> 

</div'>
#controller
def edit
  @molecular_bio = MolecularBio.find(params[:id])
  respond_to do |format|
    format.html { render :edit }
    format.js {}
  end
end
#Edit.js.erb
$('div#search').html('<%= escape_javascript(@molecular_bio.individual && @molecular_bio.individual.name) %>')

-2

 <script>
      setInterval(function(){
        $('#ID_DIV_PARA_ATUALIZAR').load('PAGINA_A_SER_CARREGADA_NA_DIV.php');
      }, 1000) /* tempo em MS para atualizar*/
    </script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.