Is it possible to showing selected value from another text box & unselected options to another text box.
For example , I have 2 text boxes & one select box with options(location_1,location_2,location_3).
So when i select option location_2 or any then selected option value will be showing in text_box_1 & remaining unselected options (location_1,location_3) that value will be appearing into another text box is text_box_2.
I have tried to do following ways but only get select option value not unselect.
HTML
<select name="pd_location[]" class="form-control">
<option value="">-- Choose --</option>
<option value="1">Location One</option>
<option value="2">Location Two</option>
<option value="3">Location Three</option>
<option value="4">Location Four</option>
</select>
Jquery
$(document).on('change', '.select_location', function() {
var get_sel_option = $(this).attr("selected_val");
var index= $(".select_location").prop('selectedIndex');
if(index){
var sel_option_test= $('.select_location option ').attr("selected","selected");
}
});
How could i get unselect option values ?
You can use jQuery.map() to get all the options not selected.
Description: Translate all items in an array or object to new array of
items.
$('select[name="pd_location[]"]').on("change", function() {
$('.text_box').val('');
var selected = this.value;
if( selected ) {
$('#text_box_1').val( selected );
var unselected = $(this).find('option:not(:first):not(:selected)').map(function() { return this.value; }).get().join(",");
$('#text_box_2').val( unselected );
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="pd_location[]" class="form-control">
<option value="">-- Choose --</option>
<option value="1">Location One</option>
<option value="2">Location Two</option>
<option value="3">Location Three</option>
<option value="4">Location Four</option>
</select>
<input name="text_box_1" id="text_box_1" class="text_box">
<input name="text_box_2" id="text_box_2" class="text_box">
Related
I have used a select box to identify the users age group. Accroding to the value of the age group, I need to display diffrent options
For eg.
<select name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
If the user selects the first option I need to display contents of 'tbl_hobbies' (mysql table) with age group 1 in another select box.
<select name="hobbies" id="hobbies">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
For the second option (26-35), I need to display 'tbl_hobbies' contents with age group 2
<select name="hobbies" id="hobbies">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
How can I achieve this using AJAX or is this possible without using AJAX?
First, add an id to age group
<select name="age_group" id="age_group">
Then
$( document ).ready(function() {
$('#age_group').change(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { group_id: $(this).val() },
dataType: "html"
})
.done(function( msg ) {
$('#hobbies').html(msg);
});
});
});
In ajax.php you get all hobbies where group_id = $_POST['group_id']
$sql = "SELECT name FROM tbl_hobbies WHERE group_id = ".$_POST['group_id'];
// Execute your query and put the result in a variable (example $result_query)
// Then loop it
foreach($result_query as $row) {
echo '<option>'.$row['name'].'</option>';
}
NB : The loop may change according to your method to retrieve data
You can simply put all option together and hide option as per user choice
<select name="hobbies" id="hobbies">
<option class="group_1">Cleaning Home</option>
<option class="group_1">Reading</option>
<option class="group_1">Travelling</option>
<option class="group_2">watching movies</option>
<option class="group_2">playing computer games</option>
<option class="group_2">chatting</option>
you can use like this
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
<select name="hobbies2" id="hobbies2">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
and your jquery script look like this
<script>
$('#age_select').on('change'.function(){
var value = $(this).val();
if ( value == '1' ) {
$('#hobbies1').show();
$('#hobbies2').hide();
} else if ( value == '2' ) {
$('#hobbies2').show();
$('#hobbies1').hide();
}
});
</script>
and if you want to pass through ajax
$('#age_select').on('change'.function(){
var value = $(this).val();
var data = 'age='+value;
$.post(
'abc.php',
data
).success(function(resp){
});
});
What you want is called "Dynamic dependent dropdowns". You can view a very good demo here.
Algorithm
1. Get all the options from database // do that using php
2. Assign classes to them according to group // do that using php
3. Put all of them in HTML // do that using php
4. Show/hide on event // do that using jQuery/javascript
Example
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option value="wm" class="grp age-grp-1">watching movies</option>
<option value="pcg" class="grp age-grp-1>playing computer games</option>
<option value="chat" class="grp age-grp-2>chatting</option>
<option value="ch" class="grp age-grp-2>Cleaning Home</option>
<option value="rea" class="grp age-grp-3>Reading</option>
<option value="tr" class="grp age-grp-4>Travelling</option>
</select>
<script type="text/javascript">
// script to execute when you change option from first dropdown
$('#age_select').on('change'.function(){
var ageGrp = $(this).val();
// first, hide all options
$('.grp').hide();
// show options only for selected group
$('.age-grp-'+ageGrp).show();
});
</script>
Hope this helps!!!
I have drop down boxes related to each other. The second dropdown values are filtered according to first dropdown box value.
Now I want to clone elements in div with class="old_div" into div with class="new_div" and add events to each new elements in div with class="new_div".
ADD
<div class="old_div" >
<select name="select1" id="select1">
<option value="1">Fruit</option>
<option value="2">Animal</option>
<option value="3">Bird</option>
<option value="4">Car</option>
</select>
<select name="select2" id="select2">
<option value="1">Banana</option>
<option value="1">Apple</option>
<option value="1">Orange</option>
<option value="2">Wolf</option>
<option value="2">Fox</option>
<option value="2">Bear</option>
<option value="3">Eagle</option>
<option value="3">Hawk</option>
<option value="4">BWM</option>
</select>
<br />
</div>
<div class="new_div">
</div>
I am able to clone elements to new div using Jquery
Jquery code:
$("#add_a").on('click',function(){
var data = $('.old_div').html();
$('.new_div').append(data);
});
$("#select1").on('change', function() {
if($(this).data('options') == undefined){
$(this).data('options',$('#select2 option').clone());
}
var id = $(this).val().trim();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});
But how can i make new elements to be related to each other.
I have created fiddle http://jsfiddle.net/6YEQx/ for reference.
You are using id with select box and making duplicates of it, this is causing problem to identify select box using id in jQuery. So use class instead of id.
HTML :
<select name="select1" class="select1">
<option value="1">Fruit</option>
<option value="2">Animal</option>
<option value="3">Bird</option>
<option value="4">Car</option>
</select>
<select name="select2" class="select2">
<option value="1">Banana</option>
<option value="1">Apple</option>
<option value="1">Orange</option>
<option value="2">Wolf</option>
<option value="2">Fox</option>
<option value="2">Bear</option>
<option value="3">Eagle</option>
<option value="3">Hawk</option>
<option value="4">BWM</option>
</select>
Also you are adding elements dynamically so need to bind change event using $(document) and on.
EDIT: also added new line in add_a button script to show all options in second select box initially.
jQuery :
$("#add_a").on('click',function(){
var data = $('.old_div').html();
$('.new_div').append(data);
// to show all options initially and remove any preselected option
$('.new_div').find('.select2:last option').show().removeAttr('selected');
});
$(document).on('change',".select1", function() {
var id = $(this).val().trim();
$(this).next('.select2').find('option').hide();
var $options = $(this).next('.select2').find('option[value=' + id + ']');
$options.show();
$options.first().attr('selected',true)
});
Demo
I am new to web development, need help with PHP coding for a form.
A form with 5 dropdown list needs to be created. The values in 1st dropdown list would be "A,B,C,D,E,F,G". If suppose "C" is selected in 1st dropdown, then 2nd dropdown will have all the values except "C" i.e; "A,B,D,E,F,G". If suppose we select "A" in second dropdown, the values shown in 3rd dropdown would be all the values except the value chosen in dropdown 1 & 2. So value in dropdown 3 would be "B,D,E,F,G.
Similarly, it will happen for other two dropdown boxes.
One way you could try is to keep track of what options need to be removed in an array. Then you could define which select controls which. Take this example:
given this html
<select id="a" var="b">
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
<select id="b" var="c" disabled>
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
<select id="c" disabled>
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
you could use this jquery
var doNotShow = [];
$('select').change( function() {
var nextSelect = $(this).attr("var");
doNotShow.push($(this).val());
$.each(doNotShow, function( index, value ) {
$("#" + nextSelect + " option[value='" + value + "']").prop('disabled', true);
});
$("#" + nextSelect).prop('disabled', false);
$(this).prop('disabled', true);
});
doNotShow.push($(this).val()); keeps track of all the previously selected values, then the $.each removes (or in this case, disables) the unwanted options from the next select in line, which is stored in the select's var property.
Here's an example.
This will be done with jquery or even with Javasript, with jQuery you can read the documentation of .change() function, and for Javascript with the onchange Attribute, try to read this question it will help you a little bit : HTML SELECT - Trigger JavaScript ONCHANGE event even when the option is not changed
I have used a select box to identify the users age group. Accroding to the value of the age group, I need to display diffrent options
For eg.
<select name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
If the user selects the first option I need to display contents of 'tbl_hobbies' (mysql table) with age group 1 in another select box.
<select name="hobbies" id="hobbies">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
For the second option (26-35), I need to display 'tbl_hobbies' contents with age group 2
<select name="hobbies" id="hobbies">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
How can I achieve this using AJAX or is this possible without using AJAX?
First, add an id to age group
<select name="age_group" id="age_group">
Then
$( document ).ready(function() {
$('#age_group').change(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { group_id: $(this).val() },
dataType: "html"
})
.done(function( msg ) {
$('#hobbies').html(msg);
});
});
});
In ajax.php you get all hobbies where group_id = $_POST['group_id']
$sql = "SELECT name FROM tbl_hobbies WHERE group_id = ".$_POST['group_id'];
// Execute your query and put the result in a variable (example $result_query)
// Then loop it
foreach($result_query as $row) {
echo '<option>'.$row['name'].'</option>';
}
NB : The loop may change according to your method to retrieve data
You can simply put all option together and hide option as per user choice
<select name="hobbies" id="hobbies">
<option class="group_1">Cleaning Home</option>
<option class="group_1">Reading</option>
<option class="group_1">Travelling</option>
<option class="group_2">watching movies</option>
<option class="group_2">playing computer games</option>
<option class="group_2">chatting</option>
you can use like this
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
<select name="hobbies2" id="hobbies2">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
and your jquery script look like this
<script>
$('#age_select').on('change'.function(){
var value = $(this).val();
if ( value == '1' ) {
$('#hobbies1').show();
$('#hobbies2').hide();
} else if ( value == '2' ) {
$('#hobbies2').show();
$('#hobbies1').hide();
}
});
</script>
and if you want to pass through ajax
$('#age_select').on('change'.function(){
var value = $(this).val();
var data = 'age='+value;
$.post(
'abc.php',
data
).success(function(resp){
});
});
What you want is called "Dynamic dependent dropdowns". You can view a very good demo here.
Algorithm
1. Get all the options from database // do that using php
2. Assign classes to them according to group // do that using php
3. Put all of them in HTML // do that using php
4. Show/hide on event // do that using jQuery/javascript
Example
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option value="wm" class="grp age-grp-1">watching movies</option>
<option value="pcg" class="grp age-grp-1>playing computer games</option>
<option value="chat" class="grp age-grp-2>chatting</option>
<option value="ch" class="grp age-grp-2>Cleaning Home</option>
<option value="rea" class="grp age-grp-3>Reading</option>
<option value="tr" class="grp age-grp-4>Travelling</option>
</select>
<script type="text/javascript">
// script to execute when you change option from first dropdown
$('#age_select').on('change'.function(){
var ageGrp = $(this).val();
// first, hide all options
$('.grp').hide();
// show options only for selected group
$('.age-grp-'+ageGrp).show();
});
</script>
Hope this helps!!!
i want to display 2x dropdown menus both will be pre populated (2nd menu "mainToon" will contain over 200 names but for the example i have shown just a few.
<select id="category" name="Category">
<option value=" "></option>
<option value=" ">-----------------</option>
<option value="Main Toon">Main Toon</option>
<option value="Alt Toon">Alt Toon</option>
<option value="Cyno Toon">Cyno Toon</option>
<option value="Super Toon">Super Toon</option>
<option value="Dust Toon">Dust Toon</option>
</select>
<select id="mainToon" name="mainToon">
<option value=" "></option>
<option value=" ">-----------------</option>
<option value="Agmar">Agmar</option>
<option value="S Tein">S Tein</option>
<option value="Karades">Karades</option>
<option value="Bad Kharma">Bad Kharma</option>
<option value="Ed jeni">Ed Jeni</option>
</select>
by default the first dropdown will show blank and i want the "mainToon" dropdown to be hidden untill any of the following are selected:
"Alt Toon",
"Cyno Toon",
"Super Toon",
"Dust Toon"
Then the form will be visable.
Can i do this by applying a .hidden css code to the dropdown and changing the class dynamically when other options are selected?
many thanks for the help.
First hide your dropdown :-
<select id="mainToon" name="mainToon" style="display:none;">
</select>
And use Jquery show hide function like this :-
$(document).ready(function(){
$("#category").change(function(){
var value = $(this).val();
if(value=="Alt Toon" || value=="Cyno Toon")
{
$("#mainToon").show();
}
else
{
$("#mainToon").hide();
}
});
});
Below show the DEMO how to show DIV based on selection value ,
$(function() {
$('#colorselector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
And Here is working demo.