Here is the aspect i want to make, each box represent each option
JavaScript
x
9
1
<label for="product">Choose:</label>
2
3
<select name="product" id="product">
4
<option value="70A">Volvo</option>
5
<option value="70B">Saab</option>
6
<option value="70C">Mercedes</option>
7
<option value="75A">Audi</option>
8
</select>
9
Is it possible to do that?
Advertisement
Answer
You need to make divs with each() from all your select options.
Then you get the click on them and change the value of your hidden select.
Edit : i commented my code.
JavaScript
1
36
36
1
// lets make divs options for the select
2
customSelect("selectOne", "customSelectOne", 1);
3
customSelect("selectTwo", "customSelectTwo", 0);
4
5
// get the click for each options created
6
$(document).on("click",".selectOption", function (event) {
7
// first, we remove class for each option div
8
$(this).parent("div:first").find('.selectOption').removeClass('checked');
9
10
const getOptionID = $(this).data('optionid'); // get value of data optionid
11
$(this).toggleClass('checked'); // add/remove checked class
12
// change value of hiddent select
13
$(this).parent("div:first").prevAll('select:first').val(getOptionID).change();
14
});
15
16
$('.hideShowSelect').click(function() {
17
$('select').toggle();
18
});
19
20
/*
21
loop that make reproduce options of your select
22
@select : selector of the select
23
@div : selector of the div that will contain the divs for each option
24
@checked : 1 to make first div checked or 0 to not
25
*/
26
function customSelect(select, div, checked) {
27
// we loop each option
28
$('select[name="' + select + '"] option').each(function(index) {
29
// check if need to add checked class if index is equal to 0 and checked equal to 1
30
const checkFirstOption = (index === 0 && checked === 1 ? ' checked' : '');
31
const optionVal = $(this).val(); // get option value
32
33
// create a div for the option with data value with option value
34
$('.' + div).append('<div class="selectOption'+ checkFirstOption +'" data-optionid="' + optionVal + '">' + optionVal + '</div>');
35
});
36
}
JavaScript
1
23
23
1
#myform {
2
max-width:450px;
3
margin-top:25px;
4
}
5
6
#myform select {
7
display:none;
8
}
9
10
#myform .selectOption {
11
color:#141414;
12
cursor:pointer;
13
display: inline-block;
14
border: 1px solid #bebebe;
15
padding:15px 17.5px;
16
border-radius:2.5px;
17
margin:5px;
18
transition: all 0.3s ease-out;
19
}
20
21
#myform .selectOption.checked {
22
border: 1px solid #111;
23
}
JavaScript
1
24
24
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<form id="myform">
3
<select name="selectOne">
4
<option value="70A">70A</option>
5
<option value="70B">70B</option>
6
<option value="70C">70C</option>
7
<option value="75A">75A</option>
8
<option value="75B">75B</option>
9
<option value="75C">75C</option>
10
</select>
11
<div class="customSelectOne"></div>
12
13
<select name="selectTwo">
14
<option value="80B">80B</option>
15
<option value="80C">80C</option>
16
<option value="80D">80D</option>
17
<option value="85B">85B</option>
18
<option value="85C">85C</option>
19
<option value="85D">85D</option>
20
</select>
21
<div class="customSelectTwo"></div>
22
</form>
23
24
<p><a href="#" class="hideShowSelect">Hide / show select</a></p>