Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Div with a Rails Link clickable?

I have a large div:

.limeskin:hover {
  background: #eee;
  cursor: pointer;
  display: block;
}

that I want to be clickable. Because I'm using Rails I need to have a Rails link be clickable: For example

<%= link_to 'Edit Your Email Address', edit_user_path %>

I'm struggling to this.

Here is the whole block:

<% @user.posts.each do |post| %>
     <div class="lists">
      <ol class="limeposts">
       <li>
        <div class="limeskin">
          <div class="limebox">
            <div class="limecost">
              <b>Price:</b>
              <%= number_to_currency(post.price, :unit => "R") %><br>
              [...]
<% end %>

Any simple legal workable answers?

Thanks

like image 871
Erin Walker Avatar asked Jun 25 '13 18:06

Erin Walker


People also ask

How do I make a div link clickable?

We simply add the onlcick event and add a location to it. Then, additionally and optionally, we add a cursor: pointer to indicate to the user the div is clickable. This will make the whole div clickable.

Can a div be clickable?

The answer is definitely yes, but you will need to write javascript code for it. We can use a click handler on the div element to make it clickable.


3 Answers

link_to can accept a block:

<%= link_to root_path do %>
  <div>Hey!</div>
<% end %>

This will surround the div with <a> tags.

Documentation: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Or if you have a big div and want to make it "clickable", using jQuery:

# html.erb
<div class="limeskin">
  <div class="limebox">
    <div class="limecost">
      <b>Price:</b>
      <%= number_to_currency(post.price, :unit => "R") %><br>
      #[...]
    </div>
  </div>
</div>

# jQuery.js
$('.limeskin').click( function(event) {
  var clicked_div = $(this);
  # do stuff with the event object and 'this' which 
  # represent the element you just clicked on
});

jsFiddle: http://jsfiddle.net/Lxw34w5o/1/

like image 159
MrYoshiji Avatar answered Oct 27 '22 10:10

MrYoshiji


Use javascript (I recommend jQuery) to make the action actually happen and CSS hover selector to modify the div background and the cursor (to change the cursor from an arrow to a hand).

like image 35
bluehallu Avatar answered Oct 27 '22 10:10

bluehallu


Using Link_to as below would be sufficient even when you have a big block including multiple tags:

<%= link_to desired_path do %>
    <div class="linkable">
        <another div>
             ... some other tags
        </another div>
    </div>
<% end %>

and I recommend you to use a different background color for mouse over events because it shows the viewer that it's a link!

In you .css file:

.linkable:hover{
    background-color: red;

}
like image 34
Aboozar Rajabi Avatar answered Oct 27 '22 11:10

Aboozar Rajabi