Need code to only run when 'other' option is selected | Javascript / swig - javascript

I want to use swig (it is my templating engine) to insert JavaScript so that the following shows only when the select option 'other' is chosen.
<div class="form-group">
<select class="custom-select">
<option selected>Select Switch Manufacturer</option>
<option value="cisco">Cisco</option>
<option value="netgear">NetGear</option>
<option value="d-link">D-link</option>
<option value="tp-link">TP-link</option>
<option value="hewlett-packard">Hewlett-Packard</option>
<option value="linksys">Linksys</option>
<option value="allied">Allied</option>
<option value="brocade">Brocade</option>
<option value="other">Other</option>
</select>
This is the code I want to only show when the user selects the 'other' option in the code above. How can I do this with Javascript / swig?
<div class="form-group">
<input type="text" class="form-control" id="switch-manufacturer" placeholder="Switch Manufacturer">
</div> <!-- Switch Manufacturer other input only show when above 'other' is selected -->

I made a pen for you.
https://codepen.io/anon/pen/WZENjo
You just need to use the value property of the <select> element.
I used a button to call the function, you can use whatever you want, including adding an onclick to the <select> element, which would look something like this.
<div class="form-group">
<select class="custom-select" id="sel" onclick="foo()">
<option selected>Select Switch Manufacturer</option>
<option value="cisco">Cisco</option>
<option value="netgear">NetGear</option>
<option value="d-link">D-link</option>
<option value="tp-link">TP-link</option>
<option value="hewlett-packard">Hewlett-Packard</option>
<option value="linksys">Linksys</option>
<option value="allied">Allied</option>
<option value="brocade">Brocade</option>
<option value="other">Other</option>
</select>

Related

JavaScript to change class on element for cascading drop-down lists

I have a website that uses PHP to look at various tables on a SQL Server, these tables provide the drop-down lists with their data. I wish to create a cascading drop-down list, below is the code that provides the HTML.
div class="col-md-4" class="form-control" id="Type">
<select>
<?php foreach ($conn->query($sqlType) as $row){echo '<option
class="'.$row['ShortCode'].'">'.$row['Type'].'</option>';} ?>
</select>
</div>
Below is the result
<option class=""></option>
<option class="Deal">Deal</option>
<option class="Price">Price</option>
<option class="Promo">Promo</option>
<option class="Promo">Promo</option>
<option class="LTD">Long term Deal</option>
On another drop-down list we wish to hide the options not selected in the first drop-down list.
<div class="col-md-4" class="form-control" id="Category">
<select">
<?php foreach ($conn->query($sqlCategory) as $row){echo '<option
class="'.$row['Type'].' hide">'.$row['Category'].'</option>';} ?>
</select>
</div>
<select">
The results are below.
<option class=" hide"></option>
<option class="Deal hide">Exceptional / Clearance</option>
<option class="Promo hide">Invoice</option>
<option class="Promo hide">Retro</option>
<option class="Listing hide">New</option>
<option class="Listing hide">Delisting</option>
<option class="Price hide">Increase</option>
<option class="Price hide">Decrease</option>
<option class="LTD hide">Off Invoice (ZXPN Only)</option>
<option class="LTD hide">Retro (No ZR88)</option>
The idea is that if for example "Listing" is selected in the first drop-down a JavaScript or JQuery script will remove the "hide" class from;
<option class="Listing hide">
You do not have a "Listing" in the first drop down, but why not use value in the first dropdown, then no need to remove classes
$(function() {
$("#Type").on("change","select", function() {
var $cat = $("#Category>select");
$cat.find("option:gt(0)").hide(); // keep first option
$("." + this.value, $cat).show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4" class="form-control" id="Type">
<select>
<option value=""></option>
<option value="Deal">Deal</option>
<option value="Price">Price</option>
<option value="Promo">Promo</option>
<option value="Listing">Listing</option>
<option value="LTD">Long term Deal</option>
</select>
</div>
<div class="col-md-4" class="form-control" id="Category">
<select>
<option class=""></option>
<option class="Deal">Exceptional / Clearance</option>
<option class="Promo">Invoice</option>
<option class="Promo">Retro</option>
<option class="Listing">New</option>
<option class="Listing">Delisting</option>
<option class="Price">Increase</option>
<option class="Price">Decrease</option>
<option class="LTD">Off Invoice (ZXPN Only)</option>
<option class="LTD">Retro (No ZR88)</option>
</select>
</div>

How to dynamically display a placeholder and deselect the other options, in a select form menu using Javascript/Jquery

I have the following drop-down menu:
<form action="...">
<select name="fruits" id="my-fruits">
<option value="" disabled selected>Select a fruit</option>
<option value="apple">apple</option>
<option value="orange">orange</option>
<option value="peach">peach</option>
</select>
<select name="animals" id="my-animals">
<option value="" disabled selected>Select an animal</option>
<option value="dog">dog</option>
<option value="cat">cat</option>
<option value="iguana">iguana</option>
></select>
</form>
What I need is when the user changes the selected option in one filed (i.e. on change event), the placeholder to be displayed on the other and vice-versa.
For example, lets say that the user had already selected orange in #my-fruits. Now, when the user changes the selection from dog to cat in #my-animals:
the placeholder "Select a fruit" to be displayed automatically on #my-fruits field, and
then, the <option value="orange"> have its "selected" attribute removed
Ira, try this:
$('#my-fruits')
.add('#my-animals')
.on('change', function () {
$(this).siblings().prop('selectedIndex', 0);
});
Also, remove unnecessary arrow in this line:
></select>
Good luck ;)
$('select').change(function() {
$(this).siblings('select').prop('selectedIndex', 0);
console.log($('option:selected', this).val())
console.log($(this).siblings('select').val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<form action="...">
<select name="fruits" id="my-fruits">
<option value="" disabled selected>Select a fruit</option>
<option value="apple">apple</option>
<option value="orange">orange</option>
<option value="peach">peach</option>
</select>
<select name="animals" id="my-animals">
<option value="" disabled selected>Select an animal</option>
<option value="dog">dog</option>
<option value="cat">cat</option>
<option value="iguana">iguana</option>
</select>
</form>
Just set the selected option of sibling select to first index
Try
$('#animals').change(function() {
$('#fruits').val("");
});

HTML select multiple option failed

I'm trying to use multiple select option in my html page
I'm using materialized css
I used the below code
But it is not working
Can anyone help me with this please
<select multiple size='6' name='list[ ]'>
<option>One</option>
<option>Two</option>
<option selected>Three</option>
<option>Four</option>
<option>Five</option>
<option selected>Six</option>
</select>
Check your syntax.
If you are using materialize css, it should work.
http://materializecss.com/forms.html
Go to "select" and check syntax.
<div class="input-field col s12">
<select multiple>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Materialize Multiple Select</label>
</div>
Also initialize select by adding
$(document).ready(function() {
$('select').material_select();
});

How to stop select box to use select2 plugin

i have many dropdowns in my page.
for dropdowns i am using select2 plugin.
i don't want to use select2 plugin for some of the dropdowns.
is there any way to do so?
i have searched on google and also have gone through their documentation but i didn't get anything related this.
this is my select box code ..
<div class="cust-input">
<select name="management[]">
<option value="0">Select Management</option>
<option value="Chairman">Chairman</option>
<option value="Vice Chairman">Vice Chairman</option>
<option value="Vice Chancellor">Vice Chancellor</option>
<option value="Secretary">Secretary</option>
<option value="Registrar">Registrar</option>
<option value="Dean">Dean</option>
<option value="Owner">Owner</option>
<option value="Other">Other</option>
</select>
</div>
You can implement that using specific class instead of common select tag element.
use:
$(".useSelect2").select2(); //either your desired name of your class or unique ID
instead of
$("select").select2(); // if you used this method, select2 will applied to all dropdowns.
Example:
<select name="management[]" class="useSelect2">
<option value="0">Select Management</option>
<option value="Chairman">Chairman</option>
<option value="Vice Chairman">Vice Chairman</option>
<option value="Vice Chancellor">Vice Chancellor</option>
<option value="Secretary">Secretary</option>
<option value="Registrar">Registrar</option>
<option value="Dean">Dean</option>
<option value="Owner">Owner</option>
<option value="Other">Other</option>
</select>
Javascript:
$(".useSelect2").select2();
Other dropdowns:
<select name="management[]" class="dontuseSelect2">
<option value="0">Select Management</option>
<option value="Chairman">Chairman</option>
<option value="Vice Chairman">Vice Chairman</option>
<option value="Vice Chancellor">Vice Chancellor</option>
<option value="Secretary">Secretary</option>
<option value="Registrar">Registrar</option>
<option value="Dean">Dean</option>
<option value="Owner">Owner</option>
<option value="Other">Other</option>
</select>
Put a class on the select elements you want to use select2 with, and then use that class to instantiate the plugin. Something like this:
$('.select2').select2();
<div class="cust-input">
<select name="management[]" class="select2">
<option value="0">Select Management</option>
<option value="Chairman">Chairman</option>
<option value="Vice Chairman">Vice Chairman</option>
<option value="Vice Chancellor">Vice Chancellor</option>
<option value="Secretary">Secretary</option>
<option value="Registrar">Registrar</option>
<option value="Dean">Dean</option>
<option value="Owner">Owner</option>
<option value="Other">Other</option>
</select>
<!-- this select will not be styled -->
<select>
<option>Foo</option>
<option>Bar</option>
</select>

Twitter bootstrap select picker add "Choose One"

Hi i'm using Bootstrap select, and i'm trying to add default option like Choose One
Here's my html code,
<div class="container">
<h3>Bootstrap Select</h3>
<select id="dataCombo" class="selectpicker">
<option data-hidden="true">Choose One</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
<option value="six">Six</option>
</select>
</div>
Also i'm using jqueryvalidation.org plugin for validation. On form submit above select option is not validating and i'm getting choose one if i print_r all posted data
Did anyone know where i'm going wrong ?
<div class="container">
<h3>Bootstrap Select</h3>
<select id="dataCombo" class="selectpicker">
<option value="" disabled selected>Choose One</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
<option value="six">Six</option>
</select>
</div>
Is this what you have wanted ...

Categories

Resources