Reputation: 131
I have devised two tables, the first without Chosen and second with Chosen. The first table is exactly how I want the second to function. A user makes a selection within the first drop down and the second drop down appears. Then a user makes a selection within the second drop down and the third appears.
I used the same functions in the first table and the second table, the only addition to the second was adding the Chosen class to the select tag. Why are the Chosen drop downs not hidden? Also, why doesn't the function process like it does in the first table, showing the next drop down after a selection in the previous is made? Like I said I want the two tables to function the same, the second just with the Chosen format. Thanks so much!
<!DOCTYPE html>
<html>
<head>
<title>Experiment</title>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".chosen-select").chosen({
disable_search_threshold: 4
});
});
</script>
<script>
function hide() {
document.getElementById("t1").style.display = "none";
document.getElementById("t2").style.display = "none";
document.getElementById("p1").style.display = "none";
document.getElementById("p2").style.display = "none";
}
function onClick1(){
document.getElementById("t1").style.display = "block";
}
function onClick2(){
document.getElementById("t2").style.display = "block";
}
function onSelect1(){
document.getElementById("p1").style.display = "block";
}
function onSelect2(){
document.getElementById("p2").style.display = "block";
}
</script>
<style type="text/css">
</style>
</head>
<body onload="hide()">
<table>
<tr>
<td>
<select onclick="onClick1()">
<option value="" disabled selected>Title</option>
<option value="clean">Clean</option>
<option value="salvage">Salvage</option>
<option value="rebuilt">Rebuilt</option>
</select>
</td>
<td >
<select id="t1" onclick="onClick2()">
<option value="" disabled selected>Title Status</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
</select>
</td>
<td>
<select id="t2">
<option value="" disabled selected>Title Stat</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
<option value="ralph jr.">Missing</option>
<option value="ralph">Missing</option>
</select>
</td>
</tr>
</table>
<table>
<tr>
<td>
<select class="chosen-select" onclick="onSelect1()">
<option value="" disabled selected>Title</option>
<option value="clean">Clean</option>
<option value="salvage">Salvage</option>
<option value="rebuilt">Rebuilt</option>
</select>
</td>
<td >
<select class="chosen-select" id="p1" onclick="onSelect2()">
<option value="" disabled selected>Title Status</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
</select>
</td>
<td>
<select class="chosen-select" id="p2">
<option value="" disabled selected>Title Stat</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
<option value="ralph jr.">Missing</option>
<option value="ralph">Missing</option>
</select>
</td>
</tr>
</table>
</body>
</html>
Upvotes: 0
Views: 3084
Reputation: 11116
You're hiding the elements before you call .chosen()
on them. This can cause issues with several UI libraries (i.e. jQueryUI buttons, and also in your case with chosen). Let .chosen()
run, then call hide()
and you'll have no issues (stylistically).
Functionally, the other issue here is that .chosen()
hides the <select>
element, so your click handler no longer gets triggered. You need to set the click handler and show/hide the parent of the <select>
since .chosen()
places a <div>
structure in the <td>
that ends up being the new select list. Here's how you can accomplish this:
<!DOCTYPE html>
<html>
<head>
<title>Experiment</title>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".chosen-select").chosen({
disable_search_threshold: 4
});
function hide() {
$("#p1").parent().hide();
$("#p2").parent().hide();
}
// call hide AFTER .chosen() has been invoked on the visible elements
hide();
});
</script>
<script>
function onSelect1(){
$("#p1").parent().show();
}
function onSelect2(){
$("#p2").parent().show();
}
</script>
<style type="text/css">
</style>
</head>
<!-- Do not call hide() on body load -->
<body>
<table>
<tr>
<td onclick="onSelect1()">
<select class="chosen-select">
<option value="" disabled selected>Title</option>
<option value="clean">Clean</option>
<option value="salvage">Salvage</option>
<option value="rebuilt">Rebuilt</option>
</select>
</td>
<td onclick="onSelect2()">
<select class="chosen-select" id="p1" >
<option value="" disabled selected>Title Status</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
</select>
</td>
<td>
<select class="chosen-select" id="p2">
<option value="" disabled selected>Title Stat</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
<option value="ralph jr.">Missing</option>
<option value="ralph">Missing</option>
</select>
</td>
</tr>
</table>
</body>
</html>
Upvotes: 3