Update a specific DIV without refreshing the page

2

How to update a DIV without giving refresh on the whole page?

Previously in rails 2.3.10, the 'remote_function' was used to update an existing div.

Now in rails 4.2, since you no longer have the remote_function, how can I do that?

Ex: When entering an ID

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

The name of the person from that ID will be displayed

div id = 'search'

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

/ div '

I tried to use this command in javascript, but I do not know much about js.

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

<scripttype="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()" %>

    
asked by anonymous 17.02.2016 / 16:43

2 answers

1

Using the 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) %>')
    
10.08.2016 / 04:15
1

Create a JS function

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

or do it right through the console

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


<div id="teste">
    
24.07.2018 / 15:20