Relisora Relisora - 6 months ago 32
HTML Question

How to make my own select

I wish to make my own select.

Actually my select looks like this :

enter image description here

JS Fiddle

and is it coded like this :

<p>Title</p>
<form>
<input type="color" onchange="colorisPanneau(value);pageSuivante();" name="coloris_panneau" list="liste_color3" id="coloris_panneau" value="#C5B9A9" class="formc" style="height:24px;width:202px;">
<datalist id="liste_color3">
<option value="#FFFFFF">
<option value="#999999">
<option value="#000000">
<option value="#582810">
</datalist>
</form>


And i want to make it look like that :

combobox then



How can i do this ?

What do i need to do to be able to personalize the interface in my
combobox
?

Where can i find documentation to be able to do so ?

Thank you very much :)

Answer

The datalist cannot be modified with css, even if you could with some css-magic, you could never get multiple items inside of the datalist options. The only solution is to create a select box from scratch.

I was bored so I made you something you can start with:

https://jsfiddle.net/wietsedevries/c05331jm/1/

html:

<div class="color-picker">
    <input id="color-input" type="hidden" name="coloris_panneau" value=""/>
    <div class="color-value list-item">Select your color</div>
    <div class="list">
        <div class="list-item" data-color="#edae0e">
            <span style="background:#edae0e;"></span>
            <span>Yellow</span>
        </div>
        <div class="list-item" data-color="#ff0000">
            <span style="background:#ff0000;"></span>
            <span>Red</span>
        </div>
        <div class="list-item" data-color="#336699">
            <span style="background:#336699;"></span>
            <span>Blue</span>
        </div>
    </div>
</div>

css:

* {
    box-sizing: border-box;
}
.color-picker {
    height: 30px;
    width: 150px;
    overflow: hidden;
    color: #666;
    background: #FFF;

}
.open {
    overflow: visible;;
}

.list {
    border: 1px solid #CCC;
    border-top: none;
    background: #FFF;
}
.list-item {
    padding: 5px;
    cursor: pointer;
}
.list-item:hover {
    background: #f1f1f1;
}
.list-item > span {
    display: inline-block;
    vertical-align: middle;
    height: 20px;
    line-height: 20px;
}
.list-item > span:first-child {
    width: 20px;
    margin-right: 5px;
}
.color-value {
    height: 30px;
    line-height: 30px;
    padding: 0 5px;
    width: 100%;
    cursor: pointer;
    border: 1px solid #CCC;
}

Javascript:

$('html').on("click",function(e) {
    $('.color-picker').removeClass('open');
});
$(document).on("click",".color-value",function(e) {
    e.stopPropagation();
    if( $('.color-picker').hasClass('open') ){
        $('.color-picker').removeClass('open');
    }else{
        $('.color-picker').addClass('open');
    }
});
$(document).on("click",".list-item",function() {
    var color = $(this).data('color');
    $('#color-input').val(color);
    $('.color-value').html( $(this).html() );

    //This is now the value of your hidden input field
    $('#value').html(color);
});
Comments