How to enable certain fields on mutliselect drop down selection in rails form
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have form which rails form which contains a multiselect dropdown. Now based on the dropdown values selected I want to enable certain fields
This is my form view
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple"
name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2
litres: <input>
</div>
<div id="Aquafina" class ="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2
litres: <input>
</div>
<div id="Kinley" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2
litres: <input>
</div>
<div id="Bindu" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2
litres: <input>
</div>
<div id="local" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][local][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2
litres: <input>
</div>
<div id="others" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][others][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2
litres: <input>
</div>
</div>
Here in the type_of_brand id field which is a multiselect, based on the selected value, I want to enable the hidden fields
So I wrote my javascript as follows:
<script type="text/javascript">
$(function() {
$("#type_of_brand").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
var data = "#" + select_val"
$(data).removeClass("hidden");
});
});
</script>
But it doesn't seem to do anything.The problem here is, it enables only one field, whichever is selected first
Any help!
javascript html ruby-on-rails
add a comment |
I have form which rails form which contains a multiselect dropdown. Now based on the dropdown values selected I want to enable certain fields
This is my form view
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple"
name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2
litres: <input>
</div>
<div id="Aquafina" class ="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2
litres: <input>
</div>
<div id="Kinley" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2
litres: <input>
</div>
<div id="Bindu" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2
litres: <input>
</div>
<div id="local" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][local][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2
litres: <input>
</div>
<div id="others" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][others][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2
litres: <input>
</div>
</div>
Here in the type_of_brand id field which is a multiselect, based on the selected value, I want to enable the hidden fields
So I wrote my javascript as follows:
<script type="text/javascript">
$(function() {
$("#type_of_brand").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
var data = "#" + select_val"
$(data).removeClass("hidden");
});
});
</script>
But it doesn't seem to do anything.The problem here is, it enables only one field, whichever is selected first
Any help!
javascript html ruby-on-rails
add a comment |
I have form which rails form which contains a multiselect dropdown. Now based on the dropdown values selected I want to enable certain fields
This is my form view
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple"
name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2
litres: <input>
</div>
<div id="Aquafina" class ="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2
litres: <input>
</div>
<div id="Kinley" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2
litres: <input>
</div>
<div id="Bindu" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2
litres: <input>
</div>
<div id="local" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][local][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2
litres: <input>
</div>
<div id="others" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][others][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2
litres: <input>
</div>
</div>
Here in the type_of_brand id field which is a multiselect, based on the selected value, I want to enable the hidden fields
So I wrote my javascript as follows:
<script type="text/javascript">
$(function() {
$("#type_of_brand").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
var data = "#" + select_val"
$(data).removeClass("hidden");
});
});
</script>
But it doesn't seem to do anything.The problem here is, it enables only one field, whichever is selected first
Any help!
javascript html ruby-on-rails
I have form which rails form which contains a multiselect dropdown. Now based on the dropdown values selected I want to enable certain fields
This is my form view
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple"
name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2
litres: <input>
</div>
<div id="Aquafina" class ="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]">
Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2
litres: <input>
</div>
<div id="Kinley" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2
litres: <input>
</div>
<div id="Bindu" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2
litres: <input>
</div>
<div id="local" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][local][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2
litres: <input>
</div>
<div id="others" class ="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][others][half_litre]"> Half
litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1
litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2
litres: <input>
</div>
</div>
Here in the type_of_brand id field which is a multiselect, based on the selected value, I want to enable the hidden fields
So I wrote my javascript as follows:
<script type="text/javascript">
$(function() {
$("#type_of_brand").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
var data = "#" + select_val"
$(data).removeClass("hidden");
});
});
</script>
But it doesn't seem to do anything.The problem here is, it enables only one field, whichever is selected first
Any help!
javascript html ruby-on-rails
javascript html ruby-on-rails
edited Jan 4 at 14:47
Mahesh Mesta
asked Jan 4 at 14:13
Mahesh MestaMahesh Mesta
3121619
3121619
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The issue is that $(this).val()
returns an array of selected values for multiple selects so you need to iterate it.
$(function() {
$("#type_of_brand").on("change", function() {
var selected_values = $(this).val();
// add hidden to divs
$(".field > div").addClass("hidden");
// remove hidden from selected divs
selected_values.forEach(function(val) {
var id = "#" + val;
$(id).removeClass("hidden");
});
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple" name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2 litres: <input>
</div>
<div id="Aquafina" class="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2 litres: <input>
</div>
<div id="Kinley" class="hidden">
<label> Cost of Kinley : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2 litres: <input>
</div>
<div id="Bindu" class="hidden">
<label> Cost of Bindu : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2 litres: <input>
</div>
<div id="local" class="hidden">
<label> Cost of Local : </label>
<input type="number" name="users[cost][local][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2 litres: <input>
</div>
<div id="others" class="hidden">
<label> Cost of Others : </label>
<input type="number" name="users[cost][others][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2 litres: <input>
</div>
</div>
What if the user select an option and unselects the option, will the particular field also hide on unseletcion?
– Mahesh Mesta
Jan 4 at 15:53
yes, you can run the code snippet and try it
– luisenrike
Jan 4 at 16:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040643%2fhow-to-enable-certain-fields-on-mutliselect-drop-down-selection-in-rails-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue is that $(this).val()
returns an array of selected values for multiple selects so you need to iterate it.
$(function() {
$("#type_of_brand").on("change", function() {
var selected_values = $(this).val();
// add hidden to divs
$(".field > div").addClass("hidden");
// remove hidden from selected divs
selected_values.forEach(function(val) {
var id = "#" + val;
$(id).removeClass("hidden");
});
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple" name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2 litres: <input>
</div>
<div id="Aquafina" class="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2 litres: <input>
</div>
<div id="Kinley" class="hidden">
<label> Cost of Kinley : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2 litres: <input>
</div>
<div id="Bindu" class="hidden">
<label> Cost of Bindu : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2 litres: <input>
</div>
<div id="local" class="hidden">
<label> Cost of Local : </label>
<input type="number" name="users[cost][local][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2 litres: <input>
</div>
<div id="others" class="hidden">
<label> Cost of Others : </label>
<input type="number" name="users[cost][others][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2 litres: <input>
</div>
</div>
What if the user select an option and unselects the option, will the particular field also hide on unseletcion?
– Mahesh Mesta
Jan 4 at 15:53
yes, you can run the code snippet and try it
– luisenrike
Jan 4 at 16:46
add a comment |
The issue is that $(this).val()
returns an array of selected values for multiple selects so you need to iterate it.
$(function() {
$("#type_of_brand").on("change", function() {
var selected_values = $(this).val();
// add hidden to divs
$(".field > div").addClass("hidden");
// remove hidden from selected divs
selected_values.forEach(function(val) {
var id = "#" + val;
$(id).removeClass("hidden");
});
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple" name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2 litres: <input>
</div>
<div id="Aquafina" class="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2 litres: <input>
</div>
<div id="Kinley" class="hidden">
<label> Cost of Kinley : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2 litres: <input>
</div>
<div id="Bindu" class="hidden">
<label> Cost of Bindu : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2 litres: <input>
</div>
<div id="local" class="hidden">
<label> Cost of Local : </label>
<input type="number" name="users[cost][local][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2 litres: <input>
</div>
<div id="others" class="hidden">
<label> Cost of Others : </label>
<input type="number" name="users[cost][others][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2 litres: <input>
</div>
</div>
What if the user select an option and unselects the option, will the particular field also hide on unseletcion?
– Mahesh Mesta
Jan 4 at 15:53
yes, you can run the code snippet and try it
– luisenrike
Jan 4 at 16:46
add a comment |
The issue is that $(this).val()
returns an array of selected values for multiple selects so you need to iterate it.
$(function() {
$("#type_of_brand").on("change", function() {
var selected_values = $(this).val();
// add hidden to divs
$(".field > div").addClass("hidden");
// remove hidden from selected divs
selected_values.forEach(function(val) {
var id = "#" + val;
$(id).removeClass("hidden");
});
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple" name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2 litres: <input>
</div>
<div id="Aquafina" class="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2 litres: <input>
</div>
<div id="Kinley" class="hidden">
<label> Cost of Kinley : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2 litres: <input>
</div>
<div id="Bindu" class="hidden">
<label> Cost of Bindu : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2 litres: <input>
</div>
<div id="local" class="hidden">
<label> Cost of Local : </label>
<input type="number" name="users[cost][local][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2 litres: <input>
</div>
<div id="others" class="hidden">
<label> Cost of Others : </label>
<input type="number" name="users[cost][others][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2 litres: <input>
</div>
</div>
The issue is that $(this).val()
returns an array of selected values for multiple selects so you need to iterate it.
$(function() {
$("#type_of_brand").on("change", function() {
var selected_values = $(this).val();
// add hidden to divs
$(".field > div").addClass("hidden");
// remove hidden from selected divs
selected_values.forEach(function(val) {
var id = "#" + val;
$(id).removeClass("hidden");
});
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple" name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2 litres: <input>
</div>
<div id="Aquafina" class="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2 litres: <input>
</div>
<div id="Kinley" class="hidden">
<label> Cost of Kinley : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2 litres: <input>
</div>
<div id="Bindu" class="hidden">
<label> Cost of Bindu : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2 litres: <input>
</div>
<div id="local" class="hidden">
<label> Cost of Local : </label>
<input type="number" name="users[cost][local][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2 litres: <input>
</div>
<div id="others" class="hidden">
<label> Cost of Others : </label>
<input type="number" name="users[cost][others][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2 litres: <input>
</div>
</div>
$(function() {
$("#type_of_brand").on("change", function() {
var selected_values = $(this).val();
// add hidden to divs
$(".field > div").addClass("hidden");
// remove hidden from selected divs
selected_values.forEach(function(val) {
var id = "#" + val;
$(id).removeClass("hidden");
});
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple" name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2 litres: <input>
</div>
<div id="Aquafina" class="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2 litres: <input>
</div>
<div id="Kinley" class="hidden">
<label> Cost of Kinley : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2 litres: <input>
</div>
<div id="Bindu" class="hidden">
<label> Cost of Bindu : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2 litres: <input>
</div>
<div id="local" class="hidden">
<label> Cost of Local : </label>
<input type="number" name="users[cost][local][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2 litres: <input>
</div>
<div id="others" class="hidden">
<label> Cost of Others : </label>
<input type="number" name="users[cost][others][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2 litres: <input>
</div>
</div>
$(function() {
$("#type_of_brand").on("change", function() {
var selected_values = $(this).val();
// add hidden to divs
$(".field > div").addClass("hidden");
// remove hidden from selected divs
selected_values.forEach(function(val) {
var id = "#" + val;
$(id).removeClass("hidden");
});
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field"> 9.
<label> Types of Packaging Water Brand Sold : </label>
<select class="form-control" id="type_of_brand" multiple="multiple" name="user[type_of_brand]">
<option value="Bisleri">Bisleri</option>
<option value="Aquafina">Aquafina</option>
<option value="Kinley">Kinley</option>
<option value="Bindu">Bindu</option>
<option value="local">Local</option>
<option value="others">Others</option>
</select>
</div><br/>
<div class="field">
<div id="Bisleri" class="hidden">
<label> Cost of Bisleri : </label>
<input type="number" name="users[cost][Bisleri][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bisleri][half_litre]"> 2 litres: <input>
</div>
<div id="Aquafina" class="hidden">
<label> Cost of Aquafina : </label>
<input type="number" name="users[cost][Aquafina][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Aquafina][half_litre]"> 2 litres: <input>
</div>
<div id="Kinley" class="hidden">
<label> Cost of Kinley : </label>
<input type="number" name="users[cost][Kinley][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Kinley][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Kinley][half_litre]"> 2 litres: <input>
</div>
<div id="Bindu" class="hidden">
<label> Cost of Bindu : </label>
<input type="number" name="users[cost][Bindu][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][Bindu][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][Bindu][half_litre]"> 2 litres: <input>
</div>
<div id="local" class="hidden">
<label> Cost of Local : </label>
<input type="number" name="users[cost][local][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][local][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][local][half_litre]"> 2 litres: <input>
</div>
<div id="others" class="hidden">
<label> Cost of Others : </label>
<input type="number" name="users[cost][others][half_litre]"> Half litres: </input>
<input type="number" name="users[cost][others][half_litre]"> 1 litres: <input>
<input type="number" name="users[cost][others][half_litre]"> 2 litres: <input>
</div>
</div>
answered Jan 4 at 15:21
luisenrikeluisenrike
1,884919
1,884919
What if the user select an option and unselects the option, will the particular field also hide on unseletcion?
– Mahesh Mesta
Jan 4 at 15:53
yes, you can run the code snippet and try it
– luisenrike
Jan 4 at 16:46
add a comment |
What if the user select an option and unselects the option, will the particular field also hide on unseletcion?
– Mahesh Mesta
Jan 4 at 15:53
yes, you can run the code snippet and try it
– luisenrike
Jan 4 at 16:46
What if the user select an option and unselects the option, will the particular field also hide on unseletcion?
– Mahesh Mesta
Jan 4 at 15:53
What if the user select an option and unselects the option, will the particular field also hide on unseletcion?
– Mahesh Mesta
Jan 4 at 15:53
yes, you can run the code snippet and try it
– luisenrike
Jan 4 at 16:46
yes, you can run the code snippet and try it
– luisenrike
Jan 4 at 16:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040643%2fhow-to-enable-certain-fields-on-mutliselect-drop-down-selection-in-rails-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown