call a javascript function from rails -


i trying populate list of cities , mark them on maps, using ajax updating div element list of cities , each city element trying call addmarker() function supposed add marker on top of google maps. using firebug can see javascript tag function call, addmarker() not being executed

what best approach forcibly call javascript function , update dom element ?

here how addmarker() looks like:

   function addmarker(lat,lng,title_new)    {       new_marker = new google.maps.marker({              position: latlng_new,              map: map,              title:title              });     } 

here how html.erb file looks

       <%= post.content %>        <script type="javascript"> addmarker( <%= post.lat_long%>, <%= post.title%>);                </script>    </div> 

the dom element being updated after document has finished loading.

i'm not clear doing, ajax , how addmarker work? sure document has been loaded before function called? regardless, when populating each div city (i'm assuming have sort of loop through response), quickest , dirtiest way directly call javascript function each element. instance in view:

<% @response.each |city| %>     ... #whatever     <script>          <%= "addmarker(#{city})" %>     </script> <% end > 

you should note however, obtrusive javascript , ugly, may want reconsider approach. there lot of articles out there on topic. here's quick 1 found. instead pass in whole result object javascript function @ top of page iterates through results , adds markers.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -