dropdown in a foreach loop - javascript

I have a list in a foreach loop and in each one there is an option to edit from a dropdown select box however when adding more than one of these select boxes the whole thing stops working when clicking the service and dropping the options down.
This is the code:
<select id="aroptions" name="aroptions" data-id="<?php echo $get['id'];?>">
<option value="none">Please Select your service</option>
<option class="tw" value="artw">Option 1</option>
<option class="aiop" value="araiop">Option 2</option>
<option class="rr" value="rr">Option 3</option>
<option class="aw" value="aw">Option 4</option>
<option class="gr" value="gr">Option 5</option>
</select>
<div class="twsettings" data-id="<?php echo $get['id'];?>" style="display:none">
<div class="form-group row" style="padding: 10px;">
<label for="username" class="col-sm-4 col-form-label">
Username:
</label>
<div class="col-sm-8">
<input type="text" class="form-control-plaintext input-lg" style="height: 39px; width:100%;"
name="twusername" id="twusername" size="45" />
</div>
<br><br>
<label for="username" class="col-sm-4 col-form-label">
Series Name:
</label>
<div class="col-sm-8">
<input type="text" data-id="<?php echo $get['id'];?>" class="form-control-plaintext input-lg" style="height: 39px; width:100%;"
name="twseries" id="twseries" size="45" />
</div>
<br><br>
<label for="default" class="col-sm-4 col-form-label">
Set as Default:
</label>
<div class="col-sm-8">
<input type="checkbox" name="main_ar_tw" data-id="<?php echo $get['id'];?>" style="width: 5%;height: 34px;" value="Yes"/><br>
</div>
</div>
</div>
The non working js is below
$('select').change(function(){
if ($(this).val() == "tw"){
$(this).closest('.twsettings').css("display", "none");
$(this).closest('.twsettings').removeClass("show");
} });
`Now i understand that for this to work each dropdown selection has to have a unique id otherwise they will all dropdown at the same time hence the data-id value but for the life of me i cannot remember how to do it.
Any help would be appreciated :)
I've created a fiddle for you to play around with. Note: The data-setting-id is different in each iteration of the loop. JSFiddle]1 (currently the dropdown will only function on the first option as the others are not added in the fiddle)

The first change that I'd make is to make your select elements have unique IDs:
<select id="aroptions<?php echo $get['id'];?>" name="aroptions" data-id="<?php echo $get['id'];?>">
That alone isn't going to do a whole lot for you though. First, you need to make sure that you're hiding all the divs before you show any of them. Then you need to make sure that when you're selecting an option, you use both the selected value and the select element's ID to determine which div to show.
For the hiding, I've given each settings div a class of option-group and in the Javascript we will run $(".option-group").hide();.
To show the correct settings div, we'll concatenate the corresponding option value with the select element ID to create the div's data-setting-id such as data-setting-id="artw<?php echo $get['id'];?>" and data-setting-id="araiop<?php echo $get['id'];?>" You will need to follow this pattern for each option group div.
Then in the Javascript, we do the same and concatenate the selected option with the element ID: var $elm = $('*[data-setting-id="' + selectedOpt + dataId + '"]');
Here's how I updated the output that you provided in the fiddle.
<select id="aroptions123" name="aroptions" data-id="123">
<option value="none">Please Select your service</option>
<option class="tw" value="artw">Option 1</option>
<option class="aiop" value="araiop">Option 2</option>
<option class="rr" value="rr">Option 3</option>
<option class="aw" value="aw">Option 4</option>
<option class="gr" value="gr">Option 5</option>
</select>
<div class="option-group twsettings" data-setting-id="artw123" style="display:none">
<div class="form-group row" style="padding: 10px;">
<label for="username" class="col-sm-4 col-form-label">
Username:
</label>
<div class="col-sm-8">
<input type="text" class="form-control-plaintext input-lg" style="height: 39px; width:100%;" name="twusername" id="twusername" size="45" />
</div>
<br><br>
<label for="username" class="col-sm-4 col-form-label">
Series Name:
</label>
<div class="col-sm-8">
<input type="text" data-id="123" class="form-control-plaintext input-lg" style="height: 39px; width:100%;" name="twseries" id="twseries" size="45" />
</div>
<br><br>
<label for="default" class="col-sm-4 col-form-label">
Set as Default:
</label>
<div class="col-sm-8">
<input type="checkbox" name="main_ar_tw" data-id="123" style="width: 5%;height: 34px;" value="Yes" /><br>
</div>
</div>
</div>
<div class="option-group aiopsettings" data-setting-id="araiop123" style="display:none">
<div class="form-group row" style="padding: 10px;">
<label for="username" class="col-sm-4 col-form-label">
Campaign ID:
</label>
<div class="col-sm-8">
<input type="text" class="form-control-plaintext input-lg" style="height: 39px; width:100%;" name="aiopid" id="aiopid" size="45" />
</div>
<br><br>
<label for="default" class="col-sm-4 col-form-label">
Set as Default:
</label>
<div class="col-sm-8">
<input type="checkbox" name="main_ar_aiop" style="width: 5%;height: 34px;" value="Yes" /><br>
</div>
</div>
</div>
Javascript:
$('select').change(function() {
var dataId = $(this).attr('data-id');
var selectedOpt = $(this).val();
// This is the settings div. Do whatever you want
var $elm = $('*[data-setting-id="' + selectedOpt + dataId + '"]');
$(".option-group").hide();
$elm.show();
//$elm.removeClass("show");
})
Here's the updated fiddle
Note that there are really a number of different ways that you could accomplish this. What I'm giving you here may not be the best or most efficient. I'm just giving you a quick answer using as much of your provided code as possible.

Related

How to show radio button after selecting option

I have a form with several inputs like below
This is the value from Jenis Layanan
When I choose value from Jenis Layanan, if I choose Corporate, I want to add new field radio button below jenis layanan. And if I choose Perorangan or Home Visit, radio button dissapear.
How can I make it work like this?
My code
<div class="p-2">
<div class="form-group">
<label class="form-label font-weight-bold" for="sub_product">Jenis Layanan</label>
<select class="form-control" name="sub_product" id="sub_product">
<option value="1">Perorangan</option>
<option value="2">Home Visit</option>
<option value="3">Corporate</option>
</select>
</div>
</div>
<div class="p-2">
<div class="form-group">
<label class="form-label font-weight-bold" for="appoinment_date">Tanggal Pelayanan</label>
<input name="appointment_date" placeholder="Tanggal Layanan" id="appointment_date" class="form-control datepicker" autocomplete="off">
</div>
</div>
You have to write some JS to your form with event listener:
document.querySelector('#sub_product').addEventListener('change', () => {
// your code
})
I made codepen for you with an example:
https://codepen.io/VeterJS/pen/BaRvYYx
Is easier with jQuery, when the Corporate option is selected the div display containing the radio buttons is changed to block and when other option is selected then the display goes back to none which hides the element.
$("select").change(function() {
let option = '';
$("select option:selected").each(function() {
option = $( this ).text();
});
if(option == 'Corporate'){
$('#radioBTNS').css('display','block')
}else{
$('#radioBTNS').css('display','none')
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="p-2">
<div class="form-group">
<label class="form-label font-weight-bold" for="sub_product">Jenis Layanan</label>
<select class="form-control" name="sub_product" id="sub_product">
<option value="1">Perorangan</option>
<option value="2">Home Visit</option>
<option value="3">Corporate</option>
</select>
</div>
</div>
<div id="radioBTNS" style="margin:10px;display:none">
<input type="radio" id="age1" name="age" value="30">
<label for="age1">Radio btn one</label><br>
<input type="radio" id="age2" name="age" value="60">
<label for="age2">Radio btn two</label><br>
</div>
<div class="p-2">
<div class="form-group">
<label class="form-label font-weight-bold" for="appoinment_date">Tanggal Pelayanan</label>
<input name="appointment_date" placeholder="Tanggal Layanan" id="appointment_date" class="form-control datepicker" autocomplete="off">
</div>
</div>

Check/uncheck radio buttons and hide option when uncheck

I have 2 radio buttons which when I click on one of them I get dropdown menu where must choose amount.
So far I was able to make it check/unchek them but the problem is that when I uncheck the radio button dropdown doesn't hide again.
I'm not very good in javascript so please help on this one. Here is the part of code
<div class="radio">
<label><input type="radio" autocomplete="off" id="paypal" name="paypal"></label> PayPal
</div>
<div class="radio">
<label><input type="radio" autocomplete="off" name="bank" id="bank"></label> Bank
</div>
<div class="form-group">
<span id="paypalamount" style="display:none">
<label class="col-sm-3 control-label" for="price"></label>
<div class="col-sm-9">
<div class="input-group">
<span class="input-group-addon">$</span>
<select id="paypalamount" required="required" class="form-control">
<option selected value="50">50</option>
</select>
</div>
</div>
</span>
<span id="bankamount" style="display:none">
<label class="col-sm-3 control-label" for="price">Please Choose Amount to Deposit</label>
<div class="col-sm-9">
<div class="input-group">
<span class="input-group-addon">$</span>
<select id="bankamount" required="required" class="form-control">
<option selected value="50">50</option>
<option value="100">100</option>
</select>
</div>
</div>
</div>
</span>
Here is JS
$(document).ready(function(){
$("#paypal").change(function(){
var showOrHide =$(this).is(':checked');
$("#paypalamount").toggle(showOrHide);
$('[name="description"]').toggleClass('#paypalamount',showOrHide )
});
$("#bank").change(function(){
var showOrHide =$(this).is(':checked');
$("#bankamount").toggle(showOrHide);
$('[name="description"]').toggleClass('#bankamount',showOrHide )
});
$("input[type='radio']").click(function()
{
var previousValue = $(this).attr('previousValue');
var name = $(this).attr('name');
if (previousValue == 'checked')
{
$(this).removeAttr('checked');
$(this).attr('previousValue', false);
}
else
{
$("input[name="+name+"]:radio").attr('previousValue', false);
$(this).attr('previousValue', 'checked');
}
});
});
And this is working demo of the code above JSFIDDLE
$(document).ready(function() {
$(":radio[name=bankpaypal]").change(function() {
if ($(this).is(':checked')) {
var name = $(this).attr('id')
$('span').hide()
$('span#' + name + 'amount').show()
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="radio">
<label>
<input type="radio" autocomplete="off" id="paypal" name="bankpaypal">
</label>PayPal
</div>
<div class="radio">
<label>
<input type="radio" autocomplete="off" name="bankpaypal" id="bank">
</label>Bank
</div>
<div class="form-group">
<span id="paypalamount" style="display:none">
<label class="col-sm-3 control-label" for="price"></label>
<div class="col-sm-9">
<div class="input-group">
<span class="input-group-addon">$</span>
<select id="paypalamount1" required="required" class="form-control">
<option selected value="50">50</option>
</select>
</div>
</div>
</span>
<span id="bankamount" style="display:none">
<label class="col-sm-3 control-label" for="price">Please Choose Amount to Deposit</label>
<div class="col-sm-9">
<div class="input-group">
<span class="input-group-addon">$</span>
<select id="bankamount1" required="required" class="form-control">
<option selected value="50">50</option>
<option value="100">100</option>
</select>
</div>
</div>
Name of radio button should be same to select only 1
ID should be unique
Get the selected radio button and show its counter select using its ID since they are similar
Use following code to check whether a radio button is checked or not
if($("input:radio[name=paypal]:checked").val())
alert("checked");
else
alert("Not checked");
Do your process of hiding and showing inside the if else conditions.
You only need to check for the value of a RadioButton as not checked. If unchecked set the style attribute display:none for the respective dropdown again in Javascript.
Just like you do it for the checked-Status of the RadioButton and just change the condition and the actions like you want the RadioButtons to behave.

AngularJS validation select box

have selectbox in form, i try make validate but not work same what i do before for input type text. What i do wrong ?
example
<div class="form-group">
<label class="control-label col-sm-3">Email<sup>*</sup></label>
<div class="col-sm-6">
<div class="input-icon-right">
<i class="fa fa-envelope"></i>
<input class="form-control" placeholder="example#example.com" type="email" ng-model="patient.email" name="email" required>
<p ng-show="myForm.email.$error.required" style="color:red" ng-if="myForm.email.$touched">Add your email</p>
</div>
</div>
</div>
this code work good and show error message when field empty.
and this code with select box no work correctly, every time select no empty but have a choice -Select-, how i can show error when no chice another.
<div class="form-group">
<label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
<div class="col-sm-6">
<select ui-select2 ng-model="patient.gender"
data-placeholder="Tên Cơ Sở"
ng-change="changeGender()"
style="width: 100%;" requied name="gender">
<option value="">-{{getWord('Select')}}-</option>
<option ng-repeat="item in genders" value="{{item.name}}">
{{getWord(item.name)}}
</option>
</select>
<p ng-show="myForm.gender.$error.required" style="color:red" ng-if="myForm.gender.$touched">Please choice gender</p>
</div>
</div>
From what I understood. You need to show error when user have not selected anything or have not touched the dropdown.
Change this line:
<p ng-show="myForm.gender.$error.required" style="color:red" ng-if="myForm.gender.$touched">Please choice gender</p>
To This:
<p style="color:red" ng-if="myForm.gender.$pristine || myForm.gender.$error.required">Please choice gender</p>
And Also there is a spelling mistake. change requied to required
Correct this too:
<select ui-select2 ng-model="patient.gender"
data-placeholder="Tên Cơ Sở"
ng-change="changeGender()"
style="width: 100%;" requied
name="gender">
To This:
<select ui-select2 ng-model="patient.gender"
ng-change="changeGender()"
style="width: 100%;" required
name="gender">
Hope this will help.
<div class="form-group">
<label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
<div class="col-sm-6">
<select ui-select2 ng-model="patient.gender"
data-placeholder="Tên Cơ Sở"
ng-change="changeGender()"
style="width: 100%;" required name="gender">
<option value="" disabled selected>-{{getWord('Select')}}-</option>
<option ng-repeat="item in genders" value="{{item.name}}">
{{getWord(item.name)}}
</option>
</select>
<p style="color:red" ng-if="!myForm.gender.$pristine && myForm.gender.$error.required">Please choice gender</p>
</div>
</div>
Can you try this ?
my final code.
but not good what i want
<div class="form-group" ng-class="{ 'has-error': myForm.gender.$pristine && myForm.gender.$invalid }">
<label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
<div class="col-sm-6">
<select ui-select2 ng-model="patient.gender" data-placeholder="Tên Cơ Sở" ng-change="changeGender(filterType)" style="width: 100%;" required name="gender">
<option value="">-{{getWord('Select')}}-</option>
<option ng-repeat="item in genders" value="{{item.name}}" >
{{getWord(item.name)}}
</option>
</select>
<p style="color:red" ng-if="myForm.gender.$pristine || myForm.gender.$error.required">Please choice gender</p>
</div>
</div>
screenshot

Need help writing a Javascript to add values

I need help in writing JavaScript code for my form. I need to add the values in the text fields(credit cards, payday loans and unsecured personal loans) and if the total gets more than or equal to $10000, on submit it should take the user to page 1, else page 2. I just want to add the first three fields, it doesn't matter what the user inputs in the other fields. Here's my form:
<form action="/fsg?pageId=0bd004f9-aabc-486e-be09-bf2621555e3e&variant=b" method="POST">
<input type="hidden" name="pageId" value="0bd004f9-aabc-486e-be09-bf2621555e3e"><input type="hidden" name="pageVariant" value="b">
<fieldset class="clearfix" style="width: 297px; height: -17px;">
<div class="lp-pom-form-field clearfix" id="container_What_state_do_you_live_in">
<label for="What_state_do_you_live_in" class="main lp-form-label" id="label_What_state_do_you_live_in">
State? *</label><select id="What_state_do_you_live_in" name="What_state_do_you_live_in" class="text form_elem_What_state_do_you_live_in">
<option value="">Select a State</option>
<option value="Arizona">Arizona</option>
<option value="California">California</option>
<option value="Florida">Florida</option>
<option value="Michigan">Michigan</option>
<option value="Nevada">Nevada</option>
<option value="New York">New York</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="Texas">Texas</option>
<option value="Virginia">Virginia</option>
<option value="I reside in another state.">I reside in another state.</option></select>
</div>
<div class="lp-pom-form-field clearfix" id="container_credit_cards">
<label for="credit_cards" class="main lp-form-label" id="label_credit_cards">Credit Cards</label><input type="text" id="credit_cards" name="credit_cards" class="text form_elem_credit_cards">
</div>
<div class="lp-pom-form-field clearfix" id="container_payday_loans">
<label for="payday_loans" class="main lp-form-label" id="label_payday_loans">Payday Loans</label><input type="text" id="payday_loans" name="payday_loans" class="text form_elem_payday_loans">
</div>
<div class="lp-pom-form-field clearfix" id="container_unsecured_personal_loans">
<label for="unsecured_personal_loans" class="main lp-form-label" id="label_unsecured_personal_loans">Unsecured Personal Loans</label><input type="text" id="unsecured_personal_loans" name="unsecured_personal_loans" class="text form_elem_unsecured_personal_loans">
</div>
<div class="lp-pom-form-field clearfix" id="container_student_loans">
<label for="student_loans" class="main lp-form-label" id="label_student_loans">Student Loans</label><input type="text" id="student_loans" name="student_loans" class="text form_elem_student_loans">
</div>
<div class="lp-pom-form-field clearfix" id="container_auto_loan">
<label for="auto_loan" class="main lp-form-label" id="label_auto_loan">Auto Loan</label><input type="text" id="auto_loan" name="auto_loan" class="text form_elem_auto_loan">
</div>
<div class="lp-pom-form-field clearfix" id="container_medical_bills">
<label for="medical_bills" class="main lp-form-label" id="label_medical_bills">Medical Bills</label><input type="text" id="medical_bills" name="medical_bills" class="text form_elem_medical_bills">
</div>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
var total = $('#Creditcards').val() + $('#Payday').val() + $('#Unsecured').val();
$('.submit').click(function(){
if(total<10000)
{window.href.location = "x"}
else{
window.href.location = "y";
}
})

Jquery get value of select in div above

I have the following HTML form. I'm trying to get the value of the .plotNumber select and also the value of the sub_category field.
Note: The user is able to add new rows to the form, so there may be multiple blocks of <div class="standard-row add-admin-row input-row">.
My code so far (which works) apart from the plot and category is:
$('.wrapper').on('input', '.inp-calculate', function(e)
{
var input = $(this);
//var plot = input.val(); // get plot id
//var category = input.val(); // get category id
var units = input.val(); // get units
$.post('checkUnits.php', {
plot_id: plot,
category_id: category,
units: units
}, function(data) {
var data = $.parseJSON(data);
if(data[0] !== true) {
alert(data);
input.focus();
input.val('');
}
});
});
I have tried the following solutions which do not work:
$(this).parent().siblings().find('.plotNumber').val();
HTML Code:
<form class="commentForm add-project-form clearfix" id="addWorksheet">
<div class="box box3 clearfix">
<div id="messages"></div>
<div class="sectionbox">
<fieldset class="clonable">
<legend><span>Worksheet</span></legend>
<p class="error-summary error-message"></p>
<div class="standard-row add-admin-row input-row">
<label class="ib">
<span class="label-stacked">Employee</span>
<span class="plain-select">
<select class="inp" data-myname="worksheet[*][employee]" name="employee">
<option value="">Select one...</option>
<option value="1">Test 1</option>
</select>
</span>
</label>
<label class="ib">
<span class="label-stacked">Week ending</span>
<span class="plain-select">
<select class="inp" data-myname="worksheet[*][week_ending]" name="week_ending">
<option value="">Select one...</option>
<option value="Sunday 22 Nov 2015">Sunday 22 Nov 2015</option>
</select>
</span>
</label>
<label class="ib">
<span class="label-stacked">Project</span>
<span class="plain-select">
<select class="inp project" data-myname="worksheet[*][project]" name="project">
<option value="">Select one...</option>
<option value="2">TEST</option>
</select>
</span>
</label>
</div>
<div class="standard-row wsheet-row input-row">
<label class="ib plothead ">
<span class="label-stacked">Plot Number</span>
<span class="plain-select">
<select class="inp plotNumber plotheadclone" data-myname="worksheet[*][plots][!][plot_number]" name="plot_number">
<option value="">Select one...</option>
<option value="6672">444</option>
<option value="6673">555</option>
<option value="6674">666</option>
</select>
</span>
</label>
<div class="plot-offset clearfix">
<div class="standard-row wsheet-row input-row" id="5438">
<label class="ib">
<span class="label-stacked">Category</span>
<select class="inp inp220 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][sub_category][]" name="sub_category">
<option value="5438">NON LABOUR</option>
</select>
</label>
<label class="ib"><span class="label-stacked">Total</span>
<input class="inp inp110 data-addup-total inp-calculate" data-myname="worksheet[*][plots][!][total_cost][]" name="total_cost" value="0.00" type="text">
<input data-myname="worksheet[*][plots][!][price][]" name="price" value="" type="hidden">
<input data-myname="worksheet[*][plots][!][number_units][]" name="number_units" value="" type="hidden">
</label>
</div>
<div class="standard-row wsheet-row input-row" id="5240">
<label class="ib">
<span class="label-stacked">Category</span>
<select class="inp inp220 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][sub_category][]" name="sub_category">
<option value="5240">1ST FIX CYLINDER</option>
</select>
</label>
<label class="ib">
<span class="label-stacked">Price</span>
<input class="inp inp110 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][price][]" name="price" value="£40.00" type="text"></label>
<label class="ib"> <span class="label-stacked">Number</span>
<input class="inp inp55 inp-calculate" data-myname="worksheet[*][plots][!][number_units][]" name="number_units" value="" type="text"></label><label class="ib"><span class="label-stacked">Total</span><input class="inp inp110 inp-disabled data-addup-total" readonly="" data-myname="worksheet[*][plots][!][total_cost][]" name="total_cost" value="£0.00" type="text"></label>
</div>
<div class="wsheet-labour-total">LABOUR: £80.00</div>
<div class="wsheet-nonlabour-total">NON LABOUR: £0.00</div>
<div class="wsheet-final-total">TOTAL CLAIM: £80.00</div>
</div>
</div>
</fieldset>
<div class="reveal-plot showingPlot"> Choose another plot</div>
</div>
<!-- end section box -->
<hr class="divider">
<div class="clonable">
<div class="clone empty"></div>
<div class="cb">
<div>
<p class="add remove-data"><i class="sprite minus2"></i> <span>Remove last row</span></p>
</div>
<div>
<p class="add add-data add-another-worksheet-employee"><i class="sprite plus2"></i> <span>Add another worksheet</span></p>
</div>
</div>
</div>
<div class="save-submit">
<button class="button" type="submit" data-url="addWorksheet.php" data-id="#addWorksheet">Submit</button>
</div>
</div>
<!-- end box -->
</form>
Can someone help find the solution?
You're not traversing far enough up the DOM with .parent() since the select is contained in a <div> three levels above .inp-calculate. How about:
$(this).closest('fieldset').find('.plotNumber').val();
Try like following. Hope this will help you.
var container=$(this).closest('.standard-row.add-admin-row.input-row');
var plot=container.find('.plotNumber').val();
var category=container.find('.inp220').val();

Categories