show select option depends on button select - javascript

I want to show dynamic select option.
<div id="residential" >
<input type="radio" value="apartment" id="type_apartment" name="type" >
<label for="type_apartment" class="radio-inline"> Apartment/Flat </label>
<input type="radio" value="bachelor" id="type_bachelor" name="type">
<label for="type_bachelor" class="radio-inline"> Bechelor </label>
</div>
<!-- this will show when apartment select -->
<div class="form-group " id="tenant-flat">
<select id="tenant-type" class="form-control" name="tenant">
<option value="">Anyone</option>
<option value="family">Family</option>
<option value="bechelor" >Bechelor</option>
</select>
</div>
<!-- this will show when bechelor is select -->
<div class="form-group " id="tenant">
<select id="tenant-type" class="form-control" name="tenant">
<option value="">Anyone</option>
<option value="student">Student</option>
<option value="job_holder" >Job Holder</option>
</select>
</div>
This select option will store value in the same table name "tenant". But if use name="tenant" in two div, it's not storing the right value. So I think, if the options show dynamically then it should work. But how can I do that? Is there another solution?

I might do it like this instead, setting an array based on the selected button, and then populating the select element with that array, making sure to clear the select element each time.
function radio_click() {
// Get elements
var select = document.getElementById("select-occupant");
var apt = document.getElementById("type_apartment");
// Reset options
select.options.length = 0;
// Set option array based on selected
if (apt.checked == true) {
var options = {
ValueA : 'Anyone',
ValueB : 'Family',
ValueC : 'Bachelor'
};
} else {
var options = {
ValueA : 'Anyone',
ValueB : 'Student',
ValueC : 'Job Holder'
};
}
// Set options in select
for(index in options) {
select.options[select.options.length] = new Option(options[index], index);
}
}
<div id="residential" >
<input type="radio" value="apartment" id="type_apartment" name="type" checked=true onclick="radio_click()">
<label for="type_apartment" class="radio-inline"> Apartment/Flat </label>
<input type="radio" value="bachelor" id="type_bachelor" name="type" onclick="radio_click()">
<label for="type_bachelor" class="radio-inline"> Bechelor </label>
</div>
<!-- this is the only one needed now -->
<div class="form-group ">
<select class="form-control" name="tenant" id="select-occupant">
<option value="">Anyone</option>
<option value="family">Family</option>
<option value="bechelor" >Bechelor</option>
</select>
</div>

Related

Some inputs disabled when checked option

Hello i would like to add function to my form when i select one option then some inputs of my form are disabled i tried to do this in jquery but something is not working.Could u help me wit this?
There is a example form:
<div class="col-4">
<label>Enabled </label>
<select name="stato" id="stato" class="form-control">
<option value="Text1">Text1</option>
<option value="Text2">Text2</option>
<option value="Text3">Text3</option>
<option value="Text4">Text4</option>
</select>
</div>
<div class="col-4" >
<label>Disabled when choose option in select</label>
<input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
</div>
and function in jquery
$(function() {
$('#data_consegna').prop('disabled', true);
$('#stato option').each(function() {
if ($(this).is('selected')) {
$('#data_consegna').prop('enabled', true);
} else {
$('#data_consegna').prop('enabled', false);
}
});
});
enter code here
$(function() {
//Triggering change event handler on element #stato
$('#stato').change(function() {
var value = $(this).val(); //Getting selected value from #stato
if (value == "") {
$('.common_disable').prop('disabled', false); //If value is empty then disable another field
} else {
$('.common_disable').prop('disabled', true); //Else enable another field
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
<label>Enabled </label>
<select name="stato" id="stato" class="form-control">
<option value="">Select One</option>
<option value="Text1">Text1</option>
<option value="Text2">Text2</option>
<option value="Text3">Text3</option>
<option value="Text4">Text4</option>
</select>
</div>
<div class="col-4" >
<label>Disabled when choose option in select</label><br>
<input id="data_consegna" type="text" class="form-control common_disable" name="data_consegna" placeholder="Data Consegna" /><br>
<input id="data_consegna2" type="text" class="form-control common_disable" name="data_consegna2" placeholder="Data Consegna2" /><br>
<input id="data_consegna3" type="text" class="form-control common_disable" name="data_consegna3" placeholder="Data Consegna3" /><br>
</div>
Please check above solution. It will enable input box at initial level. when you select one value, then it will disable input box.

How to use different attribute on change the second select option

I'm trying to change the value of my input box by changing two different select options.
The first select box is product_type with two different data attributes on the options: data-price and data-price_c.
The second Select box pay_type is to selecting between data-price or data-price_c, to update the value of lprice.
This is what I've tried:
var sp = document.getElementById('select_product');
var lp = document.getElementById('lprice');
var count = document.getElementById('count');
var fp = document.getElementById('price');
var pt = document.getElementById('paytype');
var selected_type = pt.options[pt.selectedIndex];
sp.onchange = function(){
var selected = sp.options[sp.selectedIndex];
if (selected_type === 1){
lp.value = selected.getAttribute('data-price');
} else {
lp.value = selected.getAttribute('data-price_c');
}
fp.value = "";
};
sp.onchange();
count.onchange = function(){
fp.value = lp.value * count.value;
}
<div>
<label for="select_product">Select Product</label>
<select name="product_id" id="select_product" onchange="update();">
<option value="1" data-price="10000" data-price_c="11000">Product 1</option>
<option value="2" data-price="20000" data-price_c="21000">Product 2</option>
<option value="3" data-price="30000" data-price_c="31000">Product 3</option>
</select>
</div>
<div>
<label for="paytype">Pay type:</label>
<select name="paytype" id="paytype">
<option value="1">Cash</option>
<option value="2">Dept</option>
</select>
</div>
<div>
<label for="lprice">Single Price:</label>
<input type="text" name="lprice" id="lprice" class="form-control" tabindex="1" readonly/>
</div>
<div>
<label for="count">Count:</label>
<input type="number" name="count" id="count" class="form-control" tabindex="1" />
</div>
<div>
<label for="price">Full Price:</label>
<input type="text" name="price" id="price" class="form-control" tabindex="1" readonly/>
</div>
I hope I understated you correctly what needs to be done from code and explanation:
Your first problem was that you had your selected_type outside of you onchange function so it wasn't getting the changed options onchange.
Second is that you where trying to compare values 1 & 2 with element without actually extracting those values from element (missing .value on selected_type)
I assumed you will need to update the values on your Pay type change too as well as Select Product, so there is nit trick to wrap both HTML selects into one div in this case div id="wrapper" and that will listen on both selects and call function if any of them are changed. So now you call it on wrapper.onchange.
I would also advise to put your calculation fp.value = lp.value * count.value; inside this function to update total price on change of any of those elements so I wrapped your Count: into wrapper div.
Hope this helps.
var sp = document.getElementById('select_product');
var lp = document.getElementById('lprice');
var count = document.getElementById('count');
var fp = document.getElementById('price');
var pt = document.getElementById('paytype');
var wrapper=document.getElementById('wrapper');
wrapper.onchange = function(){
var selected = sp.options[sp.selectedIndex];
var selected_type = pt.options[pt.selectedIndex].value;
if (selected_type === "1"){
lp.value = selected.getAttribute('data-price');
}
if (selected_type === "2"){
lp.value = selected.getAttribute('data-price_c');
}
fp.value = "";
fp.value = lp.value * count.value;
};
wrapper.onchange();
<div id="wrapper">
<div>
<label for="select_product">Select Product</label>
<select name="product_id" id="select_product" >
<option value="1" data-price="10000" data-price_c="11000">Product 1</option>
<option value="2" data-price="20000" data-price_c="21000">Product 2</option>
<option value="3" data-price="30000" data-price_c="31000">Product 3</option>
</select>
</div>
<div>
<label for="paytype">Pay type:</label>
<select name="paytype" id="paytype">
<option value="1">Cash</option>
<option value="2">Dept</option>
</select>
</div>
<div>
<label for="lprice">Single Price:</label>
<input type="text" name="lprice" id="lprice" class="form-control" tabindex="1" readonly/>
</div>
<div>
<label for="count">Count:</label>
<input type="number" name="count" id="count" class="form-control" tabindex="1" />
</div>
</div>
<div>
<label for="price">Full Price:</label>
<input type="text" name="price" id="price" class="form-control" tabindex="1" readonly/>
</div>

struggling with conditional statements to show/hide multiple fields on a wordpress form

My goal is to have a form on a woocommerce product page that when a certain attribute is checked then more options will show up.
More specifically. There is a question:
"How many additional dogs are you purchasing for?" and the if "one" is checked a text field with the label: "Second Dog's Name:" will appear. And if "two" is selected than "Second Dog's Name:" AND "Third Dog's Name:" will appear.
This is the code that I am working with and cannot really change the html structure because it is built with a wordpress plugin for extra product options in woocommerce.
I am able to get this close where when the fourth option is selected it does what I want and shows all three, but the first three options do nothing. But when I was writing each code, they would work until I wrote the next one.
$('.form-control').on('change', function () {
if(this.value === "option-1"){
$(".form-control-2_parent").show();
} else {
$(".form-control-2_parent").hide();
}
});
$('.form-control').on('change', function () {
if(this.value === "option-2"){
$(".form-control-2_parent").show();
$(".form-control-3_parent").show();
} else {
$(".form-control-2_parent").hide();
$(".form-control-3_parent").hide();
}
});
$('.form-control').on('change', function () {
if(this.value === "option-3"){
$(".form-control-2_parent").show();
$(".form-control-3_parent").show();
$(".form-control-4_parent").show();
} else {
$(".form-control-2_parent").hide();
$(".form-control-3_parent").hide();
$(".form-control-4_parent").hide();
}
});
.form-control-2_parent{
display:none;
}
.form-control-3_parent{
display:none;
}
.form-control-4_parent{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="wcpa_form_item wcpa_type_select form-control_parent">
<label for="select-1586051219224">Number of Additional Dogs:</label>
<div class="select">
<select name="select-1586051219224" class="form-control ">
<option value="no-addtional">No Additional Dogs</option>
<option value="option-1">One Additional Dog</option>
<option value="option-2">Two Additional Dogs</option>
<option value="option-3">Three Additional Dog</option>
</select>
<div class="select_arrow"></div>
</div>
</div>
<div class="wcpa_form_item wcpa_type_text form-control-2_parent">
<label for="text-1586038514482">Second Dog's Name</label>
<input type="text" id="text-1586038514482" class="form-control-2 " name="text-1586038514482" value="" />
</div>
<div class="wcpa_form_item wcpa_type_text form-control-3_parent">
<label for="text-1586038517583">Third Dog's Name</label>
<input type="text" id="text-1586038517583" class="form-control-3 " name="text-1586038517583" value="" /></div>
<div class="wcpa_form_item wcpa_type_text form-control-4_parent">
<label for="text-1586038516041">Fourth Dog's Name</label>
<input type="text" id="text-1586038516041" class="form-control-4 " name="text-1586038516041" value="" /></div>
I know what I am missing is probably very basic, but I am very new to writing javascript/jquery functions and complex conditional statements like this one.
Thanks in advance for any help you can give me!
Here's the solution:
// First store all out option's Elements on a list
var options = [
$(".form-control-2_parent"),
$(".form-control-3_parent"),
$(".form-control-4_parent")
]
// Create a function that hides all our options
function resetAll(){
options.forEach(function(option){
option.hide();
});
}
$('.form-control').on('change',
function (event)
{
// Get the option that was clicked
var target = $(event.currentTarget);
if(target.val())
{
// First Hide all our options, this will hide any open ones
resetAll();
// Just create a loop that shows the number of options based on the selected option
var i = 0;
while(i < target.val()){
options[i].show();
i++;
}
}
}
);
.form-control-2_parent{
display:none;
}
.form-control-3_parent{
display:none;
}
.form-control-4_parent{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="wcpa_form_item wcpa_type_select form-control_parent">
<label for="select-1586051219224">Number of Additional Dogs:</label>
<div class="select">
<select name="select-1586051219224" class="form-control ">
<option value="no-addtional">No Additional Dogs</option>
<option value="1">One Additional Dog</option>
<option value="2">Two Additional Dogs</option>
<option value="3">Three Additional Dog</option>
</select>
<div class="select_arrow"></div>
</div>
</div>
<div class="wcpa_form_item wcpa_type_text form-control-2_parent">
<label for="text-1586038514482">Second Dog's Name</label>
<input type="text" id="text-1586038514482" class="form-control-2 " name="text-1586038514482" value="" />
</div>
<div class="wcpa_form_item wcpa_type_text form-control-3_parent">
<label for="text-1586038517583">Third Dog's Name</label>
<input type="text" id="text-1586038517583" class="form-control-3 " name="text-1586038517583" value="" /></div>
<div class="wcpa_form_item wcpa_type_text form-control-4_parent">
<label for="text-1586038516041">Fourth Dog's Name</label>
<input type="text" id="text-1586038516041" class="form-control-4 " name="text-1586038516041" value="" /></div>

How to display a textarea after selecting a certain option

I have a textarea element that is not displayed. If the user selects "Other" option in the select element, the textarea should show, but it is not showing with the below code.
The following is my select element :
<select class="form-control" name="feed" id="feed" value="" style="width:540px">
<option selected="selected" disabled="true">Please select</option>
<!-- <option></option> -->
<option value="Excellent" id="Excellent"> All text is excellent</option>
<option value="Other" id="Other" onclick="showHide(this)"
>Other feedback (free text)</option
>
</select>
The following is my textarea element :
<div class="form-group">
<label for="fb_text"></label>
<textarea
class="form-control"
name="fb_text"
id="fb_text"
rows="5"
placeholder="Describe yourself here..."
value=""
style="width:540px;display:none"
></textarea>
</div>
The following is my JS :
function showHide(elm) {
var Fbtext = document.getElementById("fb_text");
if (document.getElementById("feed").value == "Other") {
Fbtext.classList.remove("hide");
}
}
You can pass value from select using onchangeevent to your function and check if that value is equals to Others if yes display textarea else hide it. Working example :
function showHide(elm) {
if (elm == "Other") {
//display textbox
document.getElementById('fb_text').style.display = "block";
} else {
//hide textbox
document.getElementById('fb_text').style.display = "none";
}
}
<select class="form-control" name="feed" id="feed" value="" onchange="showHide(this.value)" style="width:540px">
<option selected="selected" disabled="true">Please select</option>
<!-- <option></option> -->
<option value="Excellent" id="Excellent"> All text is excellent</option>
<option value="Other" id="Other">Other feedback (free text)</option>
</select>
<div class="form-group">
<label for="fb_text"></label>
<textarea class="form-control" name="fb_text" id="fb_text" rows="5" placeholder="Describe yourself here..." value="" style="width:540px;display:none"></textarea>
</div>
Add change event to the select element, see if the selected option is 'Other', hide the text area else show.
const textareaEle = document.querySelector('textarea');
document.querySelector('select').addEventListener('change', function() {
textareaEle.style.display = (this.value == 'Other') ? 'block' : 'none';
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<select class="form-control" name="feed" id="feed" value="" style="width:540px">
<option selected="selected" disabled="true">Please select</option>
<!-- <option></option> -->
<option value="Excellent" id="Excellent"> All text is excellent</option>
<option value="Other" id="Other">Other feedback (free text)</option>
</select>
<div class="form-group">
<label for="fb_text"></label>
<textarea class="form-control hide" name="fb_text" id="fb_text" rows="5" placeholder="Describe yourself here..." value="" style="width:540px;"></textarea>
</div>
I have not checked but I understand in your code you try to remove a class (hide) that you have not assigned, because you hide the textarea with the attribute style
Please try this example, I understand in your example you use bootstrap, for demonstration I have added hide
const feed = document.querySelector('#feed');
const fbtext = document.querySelector('#fb_text');
feed.addEventListener('change', handleChange);
function handleChange(event) {
const value = event.target.value;
if (value === 'Other') {
fbtext.classList.remove('hide');
return false;
}
fbtext.classList.add('hide');
}
.hide {
display: none;
}
<select
class="form-control"
name="feed"
id="feed"
value=""
style="width:540px"
>
<option selected="selected" disabled="true">Please select</option>
<option value="Excellent" id="Excellent"> Excellent</option>
<option value="Other" id="Other">Other</option>
</select>
<div class="form-group">
<label for="fb_text"></label>
<textarea
class="form-control hide"
name="fb_text"
id="fb_text"
rows="5"
placeholder="Describe yourself here..."
></textarea>
</div>

Manipulate value of child node attribute in a cloned div?

I am cloning the following div in my jsp:
<div id="row" class="">
<div class="form-group">
<div class="control-label">
<label for="name"><b> Select Name: </b></label>
</div>
<div class="control-label">
<select class="form-control" name="names" id="names">
<option value="" disabled="disabled" label="Select a name"></option>
<option value="1">Bradley</option>
<option value="2">Anderson</option>
<option value="3">Sonya</option>
</select>
</div>
<div class="control-label">
<label for="ranks"><b> Rank : </b></label>
<input type="text" value="1" name="rank" id="rank" readonly="">
</div>
</div>
</div>
I am executing the cloning in javascript:
var i = 0;
function duplicate() {
var original = document.getElementById('row');
var clone = original.cloneNode(true);
clone.id = "row" + ++i;
if(!document.getElementById('row3')){
original.parentNode.insertBefore(clone, document.getElementById('nextDiv')); //nextDiv not shown in jsp for simplification
}
}
How do I access the value of "rank" so that every time the div is cloned, the value increments by 1?
Basically I want something like clone.getElementById('rank') though I know that's not right syntax. I tried clone.setAttribute('rank', i++) but the setAttribute only accesses the attributes of the div and not the child node in the div. Is there a simpler way to do this?
Try using Array.prototype.forEach() , for loop, Element.children to increment id, for, value properties of cloned element .control-label elements child nodes . Note, for attributes at label elements should correspond to id of select , input elements; added "s" at for="names" , for="ranks" label elements
var i = 0;
function duplicate() {
var original = document.getElementById('row');
var clone = original.cloneNode(true);
clone.id = "row" + ++i;
Array.prototype.forEach.call(clone.querySelectorAll(".control-label")
, function(el, index) {
for (var j = 0; j < el.children.length; j++) {
// increment `id`
if (el.children[j].id !== "") {
el.children[j].id = el.children[j].id + i;
}
// increment `for` attribute
if (el.children[j].htmlFor !== "") {
el.children[j].htmlFor = el.children[j].htmlFor + i;
}
// increment `#ranks`-n `value`
if (el.children[j].name === "ranks") {
el.children[j].value = 1 + Number(el.children[j].value)
}
}
})
if (!document.getElementById('row3')) {
original.parentNode.insertBefore(clone,
// nextDiv not shown in jsp for simplification
document.getElementById('nextDiv'));
}
}
duplicate()
<div id="row" class="">
<div class="form-group">
<div class="control-label">
<label for="names"><b> Select Name: </b>
</label>
</div>
<div class="control-label">
<select class="form-control" name="names" id="names">
<option value="" disabled="disabled" label="Select a name"></option>
<option value="1">Bradley</option>
<option value="2">Anderson</option>
<option value="3">Sonya</option>
</select>
</div>
<div class="control-label">
<label for="ranks"><b> Rank : </b>
</label>
<input type="text" value="1" name="ranks" id="ranks" readonly="">
</div>
</div>
</div>
<div id="nextDiv"></div>

Categories

Resources