How to use a bootrap class as well as a CSS class - html

How can I use 2 different classes? I have 2 HTML option fields when the user selects repair or another in the first one. Then the second must appear. But also its need a bootstrap class: form-control.
<script>
$(function() {
$("#correctiemaatregelen").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
if (select_val === 'Other') {
$("#leveranciers").removeClass("hidden");
document.getElementById("leveranciers");
}
else if (select_val === 'Repair') {
$("#leveranciers").removeClass("hidden");
document.getElementById("leveranciers");
}
else {
$("#leveranciers").addClass("hidden");
$("#leveranciers").val("");
}
});
});
</script>
Correctie maatregelen:<br>
<select name="correctiemaatregelen" class="form-control" id="correctiemaatregelen" style="width: 300px">
<option value="--Correctie maatregel--">--Correctie maatregel--</option>
<option value="Use">Use</option>
<option value="Repair">Repair</option>
<option value="Return">Return</option>
<option value="Other">Other</option>
</select>
<br>
<select name="leveranciers" id="leveranciers" class="form-control hidden" style="width: 300px">
<?php while ($row4 = mysqli_fetch_assoc($result4)):; ?>
<option value="<?php echo $row4['statusname'];?>"><?php echo $row4['statusname'];?></option>
<?php endwhile;?>
</select>
Is there somebody that can help me with this (simple) problem?

Couldn't you just add another class to the element? If this causes problems you have to overwrite some Bootstrap styles with your own class
$(function() {
$("#correctiemaatregelen").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
if (select_val === "Other") {
$("#leveranciers").removeClass("hidden");
document.getElementById("leveranciers");
}
else if (select_val === "Repair") {
$("#leveranciers").removeClass("hidden");
$('#leveranciers').addClass("YOUR_CUSTOM_CLASS");
}
else {
$("#leveranciers").addClass("hidden");
$("#leveranciers").val("");
}
});
});
<select name="leveranciers" id="leveranciers" class="form-control YOUR_CUSTOM_CLASS hidden" style="width: 300px">
<?php while ($row4 = mysqli_fetch_assoc($result4)):; ?>
<option value="<?php echo $row4['statusname'];?>"><?php echo $row4['statusname'];?></option>
<?php endwhile;?>
</select>

Related

How to add back element after i remove it? using .removeAttr in Jquery

HI i have a example with me when if select the drop down list - price by recyclables a hidden field apppear but how can i when select the Service charger the hidden field disappear
I have try using .attr to add back but it seems to be not working
$("#select-price-mode").change(function() {
if (this.value === "Price By Recyclables") {
$('.col').removeAttr('hidden');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-row">
<div class="col">
<label for="select-price-mode" class="col-form-label">Price Mode</label>
<select class="select-price-mode custom-select-sm col-10" id="select-price-mode" required>
<option selected disabled value="">Select ....</option>
<option value="Price By Recyclables">Price By Recyclables</option>
<option value="Service Charger">Service Charger</option>
</select>
</div>
<div class="col" hidden>
<label for="select-payment-frequency" class="col-form-label">Payment Frequency</label>
<select class="select-payment-frequency custom-select-sm col-10" id="select-payment-frequency" required>
<option selected disabled value="">Select ....</option>
</select>
</div>
i think this will solve your issue
$("#select-price-mode").change(function() {
if (this.value === "Price By Recyclables") {
$('.col').removeAttr('hidden');
} else {
$('.col').attr('hidden', '');
}
});

Additional info below the main text on select option

I used select2 to produce my select option list like the snippet below.
$(function(){
$(".select2").select2();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet"/>
<div class="form-group">
<label>Minimal</label>
<select class="form-control select2" style="width: 100%;">
<option selected="selected">Alabama</option>
<option>Alaska</option>
<option>California</option>
<option>Delaware</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Washington</option>
</select>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script>
What I want is, a subtext below the main text that searchable too. when i want to search the option with value Alabama, I just need to type Okay1, but in the other hand I want to search with value Alabama only. Are there any plugins other than select2 option library that support this kind of behavior ? ( Below is the image that i expected for the result )
I have tried like the code below, but it didn't work that well :
<div class="form-group">
<label>Minimal</label>
<select class="form-control select2" style="width: 100%;">
<option selected="selected">Alabama&nbsp&nbsp<h4 style="color: grey">Okay1</h4></option>
<option>Alaska&nbsp&nbsp<h4 style="color: grey">Okay2</h4></option>
<option>California&nbsp&nbsp<h4 style="color: grey">Okay2</h4></option>
<option>Delaware&nbsp&nbsp<h4 style="color: grey">Okay3</h4></option>
<option>Tennessee&nbsp&nbsp<h4 style="color: grey">Okay4</h4></option>
<option>Texas&nbsp&nbsp<h4 style="color: grey">Okay5</h4></option>
<option>Washington&nbsp&nbsp<h4 style="color: grey">Okay6</h4></option>
</select>
</div>
You could:
encode the secondary property as an attribute of the <option> element
use a custom search function, see matchCustom in snippet below
use a custom formatter function, see formatCustom below
Working example, using state mottos as secondary attribute stored in data-foo, you can search by state name or motto:
$(function(){
$(".select2").select2({
matcher: matchCustom,
templateResult: formatCustom
});
})
function stringMatch(term, candidate) {
return candidate && candidate.toLowerCase().indexOf(term.toLowerCase()) >= 0;
}
function matchCustom(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') {
return null;
}
// Match text of option
if (stringMatch(params.term, data.text)) {
return data;
}
// Match attribute "data-foo" of option
if (stringMatch(params.term, $(data.element).attr('data-foo'))) {
return data;
}
// Return `null` if the term should not be displayed
return null;
}
function formatCustom(state) {
return $(
'<div><div>' + state.text + '</div><div class="foo">'
+ $(state.element).attr('data-foo')
+ '</div></div>'
);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet"/>
<style>
.foo { color: #808080; text-size: smaller; }
</style>
<div class="form-group">
<label>Minimal</label>
<select class="form-control select2" style="width: 100%;">
<option selected="selected" data-foo="We dare to defend our rights">Alabama</option>
<option data-foo="North to the Future">Alaska</option>
<option data-foo="Eureka">California</option>
<option data-foo="Liberty and Independence">Delaware</option>
<option data-foo="Agriculture and Commerce">Tennessee</option>
<option data-foo="Friendship">Texas</option>
<option data-foo="Bye and bye">Washington</option>
</select>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script>
I would use Bootstrap-select. It's a great library and already does every thing you want. No need to reinvent the wheel as they say!
You can search on both the title and description right out the box too.
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="form-group ">
<label>Minimal</label>
<select class="selectpicker form-control" data-live-search="true" title="Search title or description...">
<option data-subtext="description 1">Alaska</option>
<option data-subtext="description 2">California</option>
<option data-subtext="description 3">Delaware</option>
<option data-subtext="description 4">Tennessee</option>
<option data-subtext="description 5">Texas</option>
<option data-subtext="description 6">Washington</option>
</select>
</div>
</div>
</div>
</div>
CSS
.dropdown-menu > li > a {
font-weight: 700;
padding: 10px 20px;
}
.bootstrap-select.btn-group .dropdown-menu li small {
display: block;
padding: 6px 0 0 0;
font-weight: 100;
}
Here is a fiddle with a little bit of customisation

Multiple Dropdowns with Button

I currently have two dropdown boxes, I was wondering if someone can assist on how I can join the two output values into one sentence, once the user presses a button.
This is the code I am currently working with:
<!DOCTYPE html>
<html>
<body>
<form action="action_page.php">
<select id="selectcars" name="cars">
<option value="volvo" selected>Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
<div class="text-for-car" id="volvo">Your text for "Volvo"</div>
<div class="text-for-car" id="saab">Your text for "Saab"</div>
<div class="text-for-car" id="fiat">Your text for "Fiat"</div>
<div class="text-for-car" id="audi">Your text for "Audi"</div>
<style>
.text-for-car {display: none;}
#volvo {display: block;}
</style>
<script>
document.getElementById("selectcars").addEventListener("change", function () {
var id = this.options[this.selectedIndex].value;
var texts = document.getElementsByClassName("text-for-car");
for (var i = 0; i < texts.length; i++) {
if (texts[i].id == id) texts[i].style.display = "block";
else texts[i].style.display = "none";
}
})
</script>
<form>
<select id="selectcars2" name="cars2">
<option value="volvo2" selected>Volvo</option>
<option value="saab2">Saab</option>
<option value="fiat2">Fiat</option>
<option value="audi2">Audi</option>
</select>
</form>
<div class="text-for-car2" id="volvo2">Your text for "Volvo"</div>
<div class="text-for-car2" id="saab2">Your text for "Saab"</div>
<div class="text-for-car2" id="fiat2">Your text for "Fiat"</div>
<div class="text-for-car2" id="audi2">Your text for "Audi"</div>
<style>
.text-for-car2 {display: none;}
#volvo2 {display: block;}
</style>
<script>
document.getElementById("selectcars2").addEventListener("change", function () {
var id2 = this.options[this.selectedIndex].value;
var texts2 = document.getElementsByClassName("text-for-car2");
for (var i = 0; i < texts2.length; i++) {
if (texts2[i].id == id2) texts2[i].style.display = "block";
else texts2[i].style.display = "none";
}
})
</script>
</body>
</html>
Any help ? Thanks in advance!
Your demo is a bit long and I don't want to separate everything, but I made a quick demo there that should help you with your question.
http://codepen.io/paulcredmond/pen/JRxqPG
<label for="car">Car</label>
<select id="car">
<option value="Ford">Ford</option>
<option value="Fiat">Fiat</option>
<option value="Volvo">Volvo</option>
</select>
<label for="engine">Engine</label>
<select id="engine">
<option value="1.4 Petrol">1.4 Petrol</option>
<option value="1.6 Petrol">1.6 Petrol</option>
<option value="2.0 TDI">2.0 TDI</option>
</select>
<p>You have chosen a <span class="car">Ford </span> with a <span class="engine">1.4 Petrol</span> engine.</p>
$('#car').on('change', function() {
var s = $(this).val();
console.log(s);
$('p .car').text(s);
});
$('#engine').on('change', function() {
var s = $(this).val();
console.log(s);
$('p .engine').text(s);
});

How to add a class to a div on a certain option value

I was wondering how to add a class to a div if the option has the value selected. For example I'm making a website for my cousin's business, we want an easy way to shuffle through menus.
<code>
<div id="select"><form>
Menu:
<select name="select" class="form-control">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
<input type="submit" value="Submit">
</form>
</code>
I would like if the value is breakfast to hide the div drinks and show the div breakfast, and also have it reversible. Thank you here's my code on JSFiddle http://jsfiddle.net/t5R9s/
Here it is
$(document).ready(function() {
$(".form-control").change(function() {
if($(this).val() == "drinks") {
$("#drinks").show();
$("#breakfast").hide();
} else {
$("#drinks").hide();
$("#breakfast").show();
}
});
});
Here is the FIDDLE.
You can use javascript. Do you want to do it when you hit submit or just after you make the selection?
[
Fiddle
]
<script type="text/javascript">
function changeClass(){
var myVariable = document.getElementById('control').value;
if(myVariable == "drinks"){
document.getElementById('drinks').className = "";
document.getElementById('breakfast').className = "hide";
}
else if(myVariable == "breakfast"){
document.getElementById('drinks').className = "hide";
document.getElementById('breakfast').className = "";
}
}
</script>
<div id="select">
<form>Menu:
<select name="select" class="form-control" id="control" onChange="changeClass()">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
</form>
</div>
<div id="drinks">
<h2 align="center">Drinks</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>
<!--Breakfast table-->
<div id="breakfast" class="hide">
<h2 align="center">Breakfast</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>

How to avoid dojo to parse an element

I have a elemetn in my page. I don't control all javascripts used in this page, and seems that in some place my "select" element is being parsed by dojo and changed to a dijit element (dijit.form.select).
This element does not have an attribute "data-dojo-type", so I don't know why it is being changed.
Is there someway to avoid dojo parse this element?
<tr>
<th scope="row"><div dojoType="dijit.InlineEditBox">Stuff</div></th>
<td>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
</tr>
I can't see the problem you have in this jsfiddle. The InlineEditBox becomes a dijit, but the <select> does not. The example can try both Dojo 1.6.0 and Dojo 1.8.3:
HTML:
<script>
var dojoConfig = {
parseOnLoad: false
};
</script>
<script>
var v16 = "https://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js.uncompressed.js";
var v18 = "https://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js";
var dojoUrl = v18;
document.writeln("<script src='" + dojoUrl + "'></scr" + "ipt>");
</script>
<table border="1">
<tr>
<th scope="row">
<div id="inlineEditBox1" dojoType="dijit.InlineEditBox">Stuff</div>
</th>
<td>
<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
</tr>
</table>
JS:
function dojo16ready() {
if (!(dojoConfig && dojoConfig.parseOnLoad)) {
dojo.parser.parse();
}
}
function dojo18ready(parser, registry) {
function go() {
console.log("inlineEditBox1", registry.byId("inlineEditBox1"));
console.log("select1", registry.byId("select1"));
}
if (!(dojoConfig && dojoConfig.parseOnLoad)) {
parser.parse().then(go);
}
}
function init() {
console.log("dojo.version", dojo.version);
if ("function" !== typeof require) {
// Must require before onload
dojo.require("dijit.InlineEditBox");
dojo.addOnLoad(dojo16ready);
} else {
require(["dojo/parser", "dijit/registry", "dijit/InlineEditBox"], dojo18ready);
}
}
init();