Noob.joe Noob.joe - 1 month ago 9
CSS Question

How do I put a link in option 1, option 2, etc without affecting the layout?

This is being used in Bootstrap 3.0 template.

The code was working fine until I tried putting links into the options part.

The display went back to basic when I tried

<li><a href="">Option 1</a></li>
, help?

HTML code:

<a class="btn btn-default btn-select btn-select-light">
<input type="hidden" class="btn-select-input" id="" name="" value="" />
<span class="btn-select-value">Select Workshop</span>
<span class="btn-select-arrow glyphicon glyphicon-chevron-down"></span>
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
</ul>
</a>


Javascript code:

$(document).on('click', '.btn-select', function (e) {
e.preventDefault();
var ul = $(this).find("ul");
if ($(this).hasClass("active")) {
if (ul.find("li").is(e.target)) {
var target = $(e.target);
target.addClass("selected").siblings().removeClass("selected");
var value = target.html();
$(this).find(".btn-select-input").val(value);
$(this).find(".btn-select-value").html(value);
}
ul.hide();
$(this).removeClass("active");
}
else {
$('.btn-select').not(this).each(function () {
$(this).removeClass("active").find("ul").hide();
});
ul.slideDown(300);
$(this).addClass("active");
}


});

$(document).on('click', function (e) {
var target = $(e.target).closest(".btn-select");
if (!target.length) {
$(".btn-select").removeClass("active").find("ul").hide();
}


});

Answer

Nested links are illegal

Links and anchors defined by the <a> element must not be nested; an anchor element must not contain any other anchor elements.

When you add a link inside the option, you are trying to do the same. Which isn't the correct approach

Use it as

<div class="btn btn-default btn-select btn-select-light">
    <input type="hidden" class="btn-select-input" id="" name="" value="" />
    <span class="btn-select-value">Select Workshop</span>
    <span class="btn-select-arrow glyphicon glyphicon-chevron-down"></span>
    <ul>
        <li><a href="#">Option 1</a></li>
        <li><a href="#">Option 2</a></li>
        <li><a href="#">Option 3</a></li>
        <li><a href="#">Option 4</a></li>
    </ul>
</div>