user3745888
user3745888

Reputation: 6253

How to make all the bootstrap panel clickable?

I want do a bootstrap panel clickable. But not the head clickable. ALL the panel clickable. I'm trying but when I add my div into <a> all content is linkable, and I dislike this text blue by links...

<div class="panel panel-default">
    <a href="#">
        <div class="panel-body">
            ...
        </div>
        <div class="panel-footer">
            <div class="row">
              ...
            </div>
        </div>
    </a>
</div>

How can do my bootstrap panel clickable?

Thanks!

Upvotes: 2

Views: 5216

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You could style it, and better if you could add a class for clickable ones so that will not affect others :

<div class="panel panel-default clickable-panel" data-href=''>
    ...
</div>

CSS :

.clickable-panel a:hover, .clickable-panel a:active {
   color: #000;
   text-decoration: none;
}

Or if you don't want to use a tag you could add a CSS attribute cursor to the class panel that have data-href attribute that will make it clickable :

.panel[data-href]{
    cursor: pointer !important;
}

Then add data-href that contain the link you want to redirect to, example :

<div class="panel panel-default" data-href=''>
    ...
</div>

Then in your JS code add event :

$('body').on('click', '.panel[data-href]', function(){
    if(window.location.hash) {
        window.location.hash = $(this).data('href');
    } else {
        window.location.href = $(this).data('href');
    }
})

Hope this helps.

$('body').on('click', '.panel[data-href]', function(){
  if(window.location.hash) {
    window.location.hash = $(this).data('href');
  } else {
    window.location.href = $(this).data('href');
  }
})
.panel[data-href]{
  cursor: pointer !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="panel panel-default" data-href='#test'>
  <div class="panel-body">
     Go to test div
  </div>
  <div class="panel-footer">
    <div class="row">
      ...
    </div>
  </div>
</div>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<div id='test'>
  TEST DIV
</div>

Upvotes: 5

Paul Lemarchand
Paul Lemarchand

Reputation: 2096

You could just remove all styling of links from your panel :

.panel-default a:link, .panel-default a:hover, .panel-default a:visited, .panel-default a:active {
       color: #000;
       text-decoration: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class="panel panel-default">
  <a href="#">
    <div class="panel-body">
      ...
    </div>
    <div class="panel-footer">
      <div class="row">
        ...
      </div>
    </div>
  </a>
</div>

Upvotes: 0

Related Questions