Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link within a list-group-item class (itself a link) in Twitter Bootstrap

(How) Can I place a link within a twitter bootstrap "list-group-item" that is itself a link without messing up the list-group-item format? (see below)

<div class="list-group">
<a href="#" class="list-group-item active">

    <h4 class="list-group-item-heading">List group item heading</h4>
    <p class="list-group-item-text">...</p>

    <a href="https://www.facebook.com/sharer/sharer.php?u=http" target="_blank">
    Share on Facebook
    </a>

</a>
</div>
like image 489
user2975887 Avatar asked Nov 10 '13 10:11

user2975887


1 Answers

You can't use <a> tags inside other <a> tags, that's invalid HTML. Check this question.

You can make it clickable with jQuery though, with an hack like this:

HTML - use a div instead with a custom class and some data-tags:

<div class="list-group">
    <div class="list-group-item active list-group-item-linkable"
        data-link="http://www.google.com">

        <h4 class="list-group-item-heading">List group item heading</h4>

        <p class="list-group-item-text">...</p>

        <a href="https://www.facebook.com/sharer/sharer.php?u=http"
            target="_blank">

            Share on Facebook
        </a>
    </div>
</div>

CSS - make it seem like a link:

.list-group-item-linkable:hover {
    color: #555;
    text-decoration: none;
    background-color: #f5f5f5;
    cursor: pointer;
}

JS - the fun part:

$(document).ready(function() {
    $('.list-group-item-linkable').on('click', function() {
        // same window/tab:
        window.location.href = $(this).data('link');

        // new window/tab:
        //window.open($(this).data('link'));
    });

    $('.list-group-item-linkable a, .list-group-item-linkable button')
    .on('click', function(e) {
        e.stopPropagation();
    });
});

I've also created a JSFiddle.

like image 181
Pedro Moreira Avatar answered Sep 22 '22 23:09

Pedro Moreira