Show & hide html element based on selected text using jquery - html

I want to set attribute name & show select option if the selected text is not "Super Admin" and unset the attribute name & hide the select option if the selected text is "Super Admin".
<div class="form-group">
<select id="role_user" name="role" class="form-control" required>
<option value="">Select role user</option>
<option value="Admin">Admin</option>
<option value="Supervisor">Supervisor</option>
<option value="Super Admin">Super Admin</option>
</select>
</div>
The element that I want to show & hide:
<div class="form-group" id="company" style="display: none">
<label>Company</label>
<select id="company_i" class="form-control">
<option value="">Select Company</option>
#foreach ($company as $cp)
<option value="{{ $cp->id }}">{{ $cp->name }}</option>
#endforeach
</select>
</div>
JQuery code:
$(document).ready(function() {
$('#role_user').change(function() {
if ($('#role_user option:selected').text() != "Super Admin") {
$('#company').show();
$('#company_i').attr('name', 'company_id');
} else if ($('#role_user option:selected').text() == "Super Admin") {
$('#company').hide();
$('#company_i').removeAttr("name");
}
})
});
I have tried it using the code above, but only the set & remove name attribute are working. The show & hide not working.

$(document).ready(function () {
$('#role_user').change(function () {
if ($(this).val() != "Super Admin") {
$('#company').show();
$('#company_i').attr('name', 'company_id');
} else {
$('#company').hide();
$('#company_i').removeAttr("name");
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<select id="role_user" name="role" class="form-control" required>
<option value="">Select role user</option>
<option value="Admin">Admin</option>
<option value="Supervisor">Supervisor</option>
<option value="Super Admin">Super Admin</option>
</select>
</div>
<div class="form-group" id="company" style="display: none">
<label>Company</label>
<select id="company_i" class="form-control">
<option value="">Select Company</option>
</select>
</div>

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

Dropdown is not showing after a page reloads

I have three dropdowns in my page, two of them are shown perfectly while the other one is not appearing, and when looking at the element in page it gets loaded with the "style="display: none;" value even if I'm forcing it with style="display: inline;".
Here's the code:
<div class="dropdown">
<div id="myBrokenDropdown" class="dropdown-content">
<select class='filter' id="notWorkingOne">
<option value=""> Select</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</div>
<!--select button-->
<div id="select1" class="dropdown-content">
<select class='filter' id="select1Works" data-col="1">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</div>
<div id="select2" class="dropdown-content">
<select class='filter' id="select2Works" data-col="5">
<option value=""> Select</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</div>
</div>
Also, in jquery I can hide/Show the two dropdowns with the following code:
$('#select1Works').hide();
$('#select2Works').hide();
$('#notWorkingOne').hide();
$(document).ready(function() {
$("input[type='radio']").on('change', function() {
$("#select1Works").toggle($("[name=radioButton]").index(this) === 0);
$("#select2Works").val("").trigger("change");
$("#select2Works").toggle($("[name=radioButton]").index(this) === 1);
$("#select1Works").val("").trigger("change");
if ($('#select1Works').is(':visible') || $('#select2Works').is(':visible')) {
$('#notWorkingOne').show();
}
})
});
Why is the "myBrokenDropdown" not showing?
Thank you very much
It was an issue with a selector in my actual code, I missed a '#'.

How to give an alert if two selected option values are equal in html jquery?

I am trying to block selecting same input values in html.
I tried lots of jquery functions and including this,
$("#targetLanguage").change(function() {
var a = $("#sourceLanguage").val();
var b = $(this).val();
if(a === b) {
alert(a + ' matches ' + b);
}
});
I think this code should work, however does not do anything. How can I block selecting same input on second select box.
<div class="align-items-baseline d-flex flex-column form-group">
<label for="sourceLanguage"><#spring.message "sourceLanguage"/></label>
<select id="sourceLanguage" name="sourceLanguage" class="form-control" required>
<option value="" selected disabled><#spring.message 'selectSourceLanguage' /></option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectSourceLanguage" />"> ${language.languageCode}</option></#list>
</select>
</div>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="targetLanguage"><#spring.message "targetLanguage"/></label>
<select id="targetLanguage" name="targetLanguage" class="form-control" data-delayy="1000" required>
<option value="" selected disabled><#spring.message 'selectTargetLanguage' </option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectTargetLanguage"/>">${language.languageCode}</option></#list>
</select>
</div>
Try this
html
<div class="align-items-baseline d-flex flex-column form-group">
<label for="sourceLanguage"><#spring.message "sourceLanguage"/></label>
<select id="sourceLanguage" name="sourceLanguage" class="form-control" onchage="check_values()" required>
<option value="" selected disabled><#spring.message 'selectSourceLanguage' /></option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectSourceLanguage" />"> ${language.languageCode}</option></#list>
</select>
</div>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="targetLanguage"><#spring.message "targetLanguage"/></label>
<select id="targetLanguage" name="targetLanguage" class="form-control" data-delayy="1000" onchage="check_values()" required>
<option value="" selected disabled><#spring.message 'selectTargetLanguage' </option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectTargetLanguage"/>">${language.languageCode}</option></#list>
</select>
</div>
Javascript
function check_values()
{
var sourceLanguage = $("#sourceLanguage").val();
var targetLanguage = $("#targetLanguage").val();
if(sourceLanguage == targetLanguage)
{
alert(sourceLanguage + " matches "+ targetLanguage);
}
}
remove the disabled attribute from your options
$("#targetLanguage").change(function() {
var a = $("#sourceLanguage").val();
var b = $(this).val();
if(a === b) {
alert(a + ' matches ' + b);
}
console.log('a = '+ a);
console.log('b = '+ b);
console.log(this);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="sourceLanguage"><#spring.message "sourceLanguage"/></label>
<select id="sourceLanguage" name="sourceLanguage" class="form-control" required>
<option value="" selected ><#spring.message 'selectSourceLanguage' /></option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectSourceLanguage" />"> ${language.languageCode}</option></#list>
</select>
</div>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="targetLanguage"><#spring.message "targetLanguage"/></label>
<select id="targetLanguage" name="targetLanguage" class="form-control" data-delayy="1000" required>
<option value="" selected ><#spring.message 'selectTargetLanguage' </option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectTargetLanguage"/>">${language.languageCode}</option></#list>
</select>
</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>

Add a div on change of select box value not working fine

below code works fine but want to some improvement like i want to append and remove the select person div on every change of select box value
check this website http://www.travelnow.com/ 1: http://www.travelnow.com/
rooms select option feature
My HTML CODE
<select id="roomOptions">
<option value="1" selected="selected"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
</select>
<br/>
<div id="text">
<label> Adults (18+) </label>
<label> Children (0-17) </label>
</div>
<div class="select-person">
<label> Adults (18+) </label>
<select>
<option value="1"> 1 </option>
<option value="2" selected="selected"> 2 </option>
</select>
<label> Children (0-17) </label>
<select>
<option value="0" selected="selected"> 0 </option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
</select>
</div>
MY SCRIPT
$('select#roomOptions').change(function () {
$('div#text select').remove();
var val = $(this).val();
for (var x = 1; x < val; x++) {
var createSelectBox = '<select id="adults"><option value="1">1</option></select><option value="2">2</option></select><option value="3">3</option></select><select id="children"><option value="1">1</option></select><option value="2">2</option></select><option value="3">3</option></select>';
$('div#text').append(createSelectBox);
}
});
Find a solution don't know its the right and secure way or not ...
HTML
<div>
<label>Select Rooms</label>
<select id="txtTickets">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
</div>
<div style="width:200px; float:left">
<div style="width:100px; float:left;">
<label>Adult</label><br>
<select>
<option>one</option>
<option>two</option>
<option>three</option>
</select>
</div>
<div style="float:left; width:100px">
<label>Children</label><br>
<select>
<option>one</option>
<option>two</option>
<option>three</option>
</select>
</div>
</div>
<div style="clear:both"></div>
<div id="selectContainer"></div>
The Script
$('#txtTickets').change(function() {
var val = parseInt($(this).val(), 10);
var html = '';
for (var i = 1; i < val; i++) {
html += '<select class="adults"><option>One</option><option>Two</option></select><select class="childs"><option>One</option><option>Two</option></select><br>';
}
$('#selectContainer').html(html);
});