简体   繁体   中英

Click a rails link_to with jQuery

I'm trying to click a rails ajax link using jquery by clicking on a google map marker but can't figure out how to do it. Here's the link :

sidelistings div

<% @properties.each_with_index do |property,index| %>
          <div class="sidelisting" id="<%= index %>">
            <div><%= link_to image_tag("http://432.mlsimages.movoto.com/0#{property.mls.last(2)}/tn/#{property.mls}_0.jpg"), {:action => 'show', :id => property.id}, :remote => true %>  </div>

I have a google map with 20 markers on it which match the 20 properties in the above sidelistings div. I want to be able to click on the map marker and that will click the above link for it's corresponding property. I have made the id s of the 20 sidelistings match the indexes of the array of map markers. Here's my attempt at the jquery in the google maps code:

google.maps.event.addListener(markerArray[i], 'click', function(){
         var element = this.id;
         var sidelisting = document.getElementById(element);
         $('sidelisting:a').click();
     });

The above code doesn't throw any errors in the console but clicking on the map marker does nothing, what am I doing wrong?

Got it figured out, I was getting a couple of things wrong, here's the working code:

google.maps.event.addListener(markerArray[i], 'click', function(){
         var index = markerArray.indexOf(this);
         $('#' + index + ' a').click();
});

I didn't know I had to construct the selector within the $() that way with the plus signs and the space before the a tag is also required

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM