Problems with removeClass() - html

On the front-end I'm asking the user 2 questions, the first one visible, the second will be visible depending on the firs answer. But somehow my removeClass is not working as expected (not working at all)
<div class="form-group col-md-4 md-form">
<label for="savingcontratos">Cancelamento de Contratos?</label>
<select class="mdb-select md-form" id="savingcontratos" searchable="Pesquisar">
<option value=""></option>
<option value="0">Sim</option>
<option value="1">Não</option>
</select>
</div>
<div class="form-group col-md-4 md-form invisible" id="divcontratos">
<label for="txtcontratos">Insira os contratos envolvidos</label>
<input type="text" class="form-control" id="txtcontratos" value="0">
</div>
And the functions I'm using are:
$('#savingfinanceiro').on('change',function () {
var component = $("#divfinanceiro");
makevisible($('#savingfinanceiro').val(), component);
});
function makevisible(value,component){
if (value === "0"){
component.removeClass("invisible");
}
}
Any help would be greatly appreciated.

as pointed out in the comments #savingfinanceiro and #divfinanceiro do not exist in your html. You must have overlooked that. In the following snippet I have fixed that. There, #savingfinanceiro, from the <select>, was changed to #savingcontratos and var component = $("#divfinanceiro");, the 'to-hide' <div> was changed to var component = $("#divcontratos");. After applying these changes your code should work as expected.
$('#savingcontratos').on('change', function() {
var component = $("#divcontratos");
makevisible($('#savingcontratos').val(), component);
});
function makevisible(value, component) {
if (value === "0") {
component.removeClass("invisible");
} else {
component.addClass("invisible");
}
}
.invisible {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group col-md-4 md-form">
<label for="savingcontratos">Cancelamento de Contratos?</label>
<select class="mdb-select md-form" id="savingcontratos" searchable="Pesquisar">
<option value=""></option>
<option value="0">Sim</option>
<option value="1">Não</option>
</select>
</div>
<div class="form-group col-md-4 md-form invisible" id="divcontratos">
<label for="txtcontratos">Insira os contratos envolvidos</label>
<input type="text" class="form-control" id="txtcontratos" value="0">
</div>
Hope this helps!

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', '');
}
});

How to get the text of selected option instead of value

Here's the body of my html:
<form class="p-3">
<div class="form-group">
<label>Full Name</label>
<input id="inputName" type="text" oninput="Update(this.value, 'namec')"
class="form-control"
placeholder="Robert">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label>Contact Number</label>
<input id="inputNumber" type="text" oninput="Update(this.value,
'numberc')" class="form-control"
placeholder="09*********">
</div>
<div class="form-group col-md-6">
<label>Email Address</label>
<input id="inputEmail" type="email" oninput="Update(this.value, 'emailc')"
class="form-control"
placeholder="email#gmail.com">
</div>
</div>
<div class="form-group">
<label>Full Address</label>
<input type="text" class="form-control" placeholder="1234 Main St"
oninput="Update(this.value, 'addressc')">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label>Mode of Payment</label>
<select id="inputMOP" class="form-control" oninput="Update(this.value, 'mopc')">
<option selected>Select Payment</option>
<option>GCash</option>
<option>Bank Payment</option>
<option>Cash On Delivery</option>
</select>
</div>
<div class="form-group col-md-6">
<label>Shipping Option</label>
<select id="inputMOD" class="form-control" oninput="Update(this.value,
'modc')">
<option selected>Select Delivery</option>
<option>Standard Delivery</option>
<option>J&T Express</option>
<option>Ninjavan Express</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-8">
<label>Item Name</label>
<select id="inputItem" class="itemInput form-control"
oninput="Update(this.value, 'itemc')">
<option selected>Select Item</option>
<option value="155.00">Hygienix Alcohol 1L</option>
<option value="180.00">Protect Plus Alcohol 1 Gal</option>
<option value="215.00">Guardian Alcohol 1 Gal</option>
</select>
</div>
<div class="form-group col-md-4">
<label>Quantity</label>
<input id="inputQuantity" type="text" oninput="Update(this.value,
'quantityc')"
class="itemQuantity form-control" placeholder="0"><br>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-3">
<h6 class="mt-2">Total Price: ₱ </h6>
</div>
<div class="form-group col-md-3">
<input id="itemPrice" type="text" oninput="Update(this.value, 'pricec')"
class="form-control" placeholder="0" readonly>
</div>
<div class="form-group col-md-6">
<button id="placeOrder" class="btn btn-primary btn-block float-
right">Place Order</button>
</div>
</div>
</form>
Jquery to calculate the total price:
<script>
$(document).ready(function () {
$('select.itemInput').on('change', function () {
$('.itemQuantity').text($('#inputQuantity').val(0));
$('.itemQuantity').on('change', function () {
$('#itemPrice').val(
$('#inputItem').val() * $('#inputQuantity').val() + ".00"
);
});
});
});
</script>
Variable declaration to add to the database:
var cName = $('#inputName').val();
var cNumber = $('#inputNumber').val();
var cEmail = $('#inputEmail').val();
var cAddress = $('#inputAddress').val();
var cMop = $('#inputMop').val();
var cMod = $('#inputMod').val();
var cItem = $('#inputItem').find(":selected").text();
var cQuantity = $('#inputQuantity').val();
var cPrice = $('#itemPrice').val();
The problem is that I cant get the text of the selected option from the #inputItem. It only gets the value from the option. Another thing, I can't also get the total price which is under the ID of #itemPrice.
How can I get the text instead of value of #inputItem? Also, the value of #itemPrice?
EDIT:
Finally solved it!! But I have a new problem, I cant add data with a "#gmail.com" but it works when it does not have it. How can I do add data with "#email.com"?
Use the following script for the desired results. Also if you don't have any function to call onChange, kindly remove those from your input fields, from html. They will keep on throwing errors in console.
$(document).ready(function () {
$('.itemInput').on('change', function () {
// tempItemName stores the selected item name
var tempItemName = $('.itemInput option:selected').text();
totalPriceCalc();
});
$('.itemQuantity').on('change', function(){
totalPriceCalc();
});
function totalPriceCalc () {
var tempInputQty = $('#inputQuantity').val();
var tempItemPrice = $('#inputItem option:selected').val();
var tempTotalCost = (tempInputQty * tempItemPrice) + '.00';
$('#itemPrice').val(tempTotalCost);
}
});

How to hide a label of a dropdown with hide() function

I have 2 dropdowns (EmployeeType, Type) and a textbox (Rate) and I just want to hide "Type" and "Rate" according to the selected value of "EmployeeType". I've done it with no issues to the "Rate", but for the "Type" only the dropdown hides not the label. Can anyone help me to hide the label of the dropdown as well?
<div class="control-group">
<label class="control-label" for="Designation">Employee Type</label>
<div class="controls">
<select name="Designation" onchange="ajaxRate(this)" id="Designation">
<option value="">--Select Employee Type--</option>
<option value="Center">Center</option>
<option value="Visiting">Visiting</option>
<option value="Other">Other</option>
</select>
<span id="ajax_img3"></span>
</div>
<!-- designation-->
<br/>
<!-- 2019-03-18 Visiting Type -->
<div class="control-group">
<label class="control-label" for="Type">Type</label>
<div class="controls">
<select name="Type" id="TypeValue">
<option value="">--Select Type--</option>
<option value="Yes">Regular</option>
<option value="No">Visiting</option>
</select>
</div>
</div>
<div class="control-group" id='rate'>
<label class="control-label" for="Rate">Hourly Rate</label>
<div class="controls">
<input type="number" name="Rate" id='rateValue'/>
</div>
</div>
#include('includes.footer')
<script>
$(document).ready(function () {
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x) {
if (x.value == 'Center') {
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
} else {
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
}
}
$( document ).ready(function()
{
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x)
{
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
$("#TypeValue").parents(".control-group").show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
$("#TypeValue").parents(".control-group").hide();
}
}
You have to hide full div using parents or you have to add any id to main div to hide.
function ajaxRate(x) {
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').hide();
}
}

Hierarchical Select Using Aurelia

I'm writing a mobile application using Aurelia (Cordova, Typescript, HTML5, & Bootstrap) and I need to do a hierarchical select where the selection from one SELECT list filters the options in the next SELECT list. Does anyone know how to do this in Aurelia? My code with the bindings are below. The list in selRatedItems needs to be filtered by what is selected in selCategories. Thanks for any help.
<div class="row">
<div class="form-group form-group-sm">
<label for="selCategory" class="col-sm-2 control-label">Category</label>
<div class="col-sm-10">
<select class="form-control" id="selCategory" value.bind="selectedCategory" required>
<option value="">Select a category...</option>
<option repeat.for="option of categories" model.bind="option">${option.name}</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group form-group-sm">
<label for="selRatedItem" class="col-sm-2 control-label">Rated Item</label>
<div class="col-sm-10">
<select class="form-control" id="selRatedItem" value.bind="selectedItem" required>
<option value="">Select an item...</option>
<option repeat.for="option of selectedCategory.rateditems" model.bind="option.rateditems.id">${option.rateditems.name}</option>
</select>
</div>
</div>
</div>
Here's how I resolved this...
First, I had to add a computed property in the view model that was based off of a property bound to the selCategory SELECT list from above.
Added import statement:
import { computedFrom } from 'aurelia-framework';
Added property:
#computedFrom('selectedCategory')
get rateditems() {
if (this.selectedCategory && this.selectedCategory.rateditems) {
return Object.keys(this.selectedCategory.rateditems).map(key => this.selectedCategory.rateditems[<any>key]);
}
else {
var array: any[] = [];
return array;
}
}
Then, I bound the SELECT list that is to be filtered, in this case, selRatedItem, to the computed property.
Newly bound SELECT list:
<div class="row">
<div class="form-group form-group-sm">
<label for="selRatedItem" class="col-sm-2 control-label">Rated Item</label>
<div class="col-sm-10">
<select class="form-control" id="selRatedItem" value.bind="selectedItem" required>
<option value="">Select an item...</option>
<option repeat.for="item of rateditems" model.bind="item.id">${item.name}</option>
</select>
</div>
</div>
</div>

How to disable a input field according to another select field value in bootstrap?

I am using bootstrap, I wanted to know is there something like a feature in bootstrap to do this?
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Flooring material </label>
<div class="col-sm-6">
<select class="form-control" name="flooring_meterial">
<option value="0">-Select-</option>
<option value="1" >Earth,sand</option>
<option value="2">Dung</option>
<option value="3">Wood/planks</option>
<option value="4">Parquet or polished wood</option>
<option value="5">other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="f_m_other">
</div>
</div>
I want to activate this bellow input field, if the above select field value is "other"
Since I could not able to find a short cut for this using bootstrap, I thought to write this in native javascript.
function disable(select_val,input_id) {
var e = document.getElementById(select_val);
var strUser = e.options[e.selectedIndex].value;
if(strUser === "100"){
document.getElementById(input_id).disabled = false;
}
else{
document.getElementById(input_id).value = document.getElementById(input_id).defaultValue;
document.getElementById(input_id).disabled = true;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Principle mode of water supply</label>
<div class="col-sm-6">
<select class="form-control" name="water_supply" id="water_supply" onchange="disable('water_supply', 'w_s_other')">
<option value="0">-Select-</option>
<option value="1">Shared/ public well</option>
<option value="4">Private pipe line</option>
<option value="5">Stream/river</option>
<option value="100" >Other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="w_s_other" id="w_s_other" disabled value="">
</div>
</div>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">x2</label>
<div class="col-sm-6">
<select class="form-control" name="water_supply" id="x2" onchange="disable('x2', 'x2other')">
<option value="0">-Select-</option>
<option value="1">Shared/ public well</option>
<option value="5">Stream/river</option>
<option value="100" >Other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="w_s_other" id="x2other" disabled value="">
</div>
</div>
Try using change event at select element to set input disabled to true if value of select element is "5" with .prop()
$("select").change(function() {
$("+ input", this).prop("disabled", !(this.value === "5"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Flooring material </label>
<div class="col-sm-6">
<select class="form-control" name="flooring_meterial">
<option value="0">-Select-</option>
<option value="1" >Earth,sand</option>
<option value="2">Dung</option>
<option value="3">Wood/planks</option>
<option value="4">Parquet or polished wood</option>
<option value="5">other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="f_m_other" disabled="true">
</div>
</div>