Is there a chance to have a combobox, which every time I select something in there, it opens a new one, etc, until no more data to select?
(options must not be repeated)
Combo1 - (opt1-opt2-opt3) - selected opt1
Combo2 - (opt2-opt3) - selected opt3
Combo3 - (opt2)
Comboboxes should be populated by querys using php and mysql.
How can I do it ?
Cheers!
Here is some jquery that will do this:
HTML
<select id="combo1" class="combo" data-index="1">
<option></option>
<option val="Opt1">Opt1</option>
<option val="Opt2">Opt2</option>
<option val="Opt3">Opt3</option>
</select>
Javascript
$('body').on('change', '.combo', function() {
var selectedValue = $(this).val();
if ($(this).find('option').size() > 2) {
var newComboBox = $(this).clone();
var thisComboBoxIndex = parseInt($(this).attr('data-index'), 10);
var newComboBoxIndex = thisComboBoxIndex + 1;
$('.parentCombo' + thisComboBoxIndex).remove();
if (selectedValue !== '') {
newComboBox.attr('data-index', newComboBoxIndex);
newComboBox.attr('id', 'combo' + newComboBoxIndex);
newComboBox.addClass('parentCombo' + thisComboBoxIndex);
newComboBox.find('option[val="' + selectedValue + '"]').remove();
$('body').append(newComboBox);
}
}
});
Demo
Explanation
// Adds an event listener to each combo box that is created dynmically
$('body').on('change', '.combo', function() {
// Gets this selected value
var selectedValue = $(this).val();
// Checks if there are enough options left to create another combo box
if (selectedValue !== '' && $(this).find('option').size() > 2) {
// Clones the just selected combo box and get the current and next combo index
var newComboBox = $(this).clone();
var thisComboBoxIndex = parseInt($(this).attr('data-index'), 10);
var newComboBoxIndex = thisComboBoxIndex + 1;
// Removes any "children" combo boxes
$('.parentCombo' + thisComboBoxIndex).remove();
// Checks if a blank value is not selected to create another combo box
if (selectedValue !== '' && $(this).find('option').size() > 2) {
// Gives this new combo box the correct id, index, parent class
newComboBox.attr('data-index', newComboBoxIndex);
newComboBox.attr('id', 'combo' + newComboBoxIndex);
newComboBox.addClass('parentCombo' + thisComboBoxIndex);
// Removes the selected option from the new combo box
newComboBox.find('option[val="' + selectedValue + '"]').remove();
// Adds the combo box to the page. Disco!
$('body').append(newComboBox);
}
}
});
Related
Here are the two select boxes. I am trying to get the value of C to pass on in a form. Select box C is populated from the ajax call below but I can't seem to get a useable value to pass on to the form. (In its simplest form I am looking for something like var A= 'select box C') then I can send 'A' in the form:
Many thanks in advance people x
<select name="list-select" id="list-select">
<option value="">Please select..</option>
<option value="Chemical">Chemical</option>
<option value="Hardware">Hardware</option>
</select>
<select name="C" id="C"></select>
Here is the jquery bit:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function ($) {
var list_target_id = 'C'; //first select list ID
var list_select_id = 'list-select'; //second select list ID
var initial_target_html = '<option value="">Please select a colour...</option>'; //Initial prompt for target select
$('#' + list_target_id).html(initial_target_html); //Give the target select the prompt option
$('#' + list_select_id).change(function (e) {
//Grab the chosen value on first select list change
var selectvalue = $(this).val();
//Display 'loading' status in the target select list
$('#' + list_target_id).html('<option value="">Loading...</option>');
if (selectvalue == "") {
//Display initial prompt in target select if blank value selected
$('#' + list_target_id).html(initial_target_html);
} else {
//Make AJAX request, using the selected value as the GET
$.ajax({
url: 'http://www.mypropertyviewing.com/Client_order_form_v1.1/selector.php?svalue=' + selectvalue,
success: function (output) {
$('#' + list_target_id).html(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
}
});
}
});
});
</script>
use
selectvalue=this.value //instead of
selectvalue =$(this).val();
I've got multiple < select > dropdown lists. Every dropdown list has its own id and is assigned like this:
for($i=0; $i<12; $i++) {
echo '<select class="selectpicker" id="select$i">
<option value="x">text</option>
</select>';
}
In jQuery, how do i get the selected option of the select dropdown list i have clicked? So far I've got the following code:
<script>
for (i = 0; i < 12; i++) {
$("#select" + i + "").change(function () {
alert("Im inside a select box!");
var selectValue = document.getElementById('select' + i + '').value;
var selectOption = $("#select" + i + " option[value=" + selectValue + "]").text();
alert(selectOption);
});
}
</script>
It does alert "Im inside a select box!" whichever select box i click. But it doesn't alert the selected option though. Any idea what I'm doing wrong?
to start off in your php you have "for($i=0; $i<10; $i++) {" and in your js you have "for (i = 0; i < 12; i++) {", shouldn't the "i < 12" be "i < 10" in your js? it may mismatch the select id
This is how I fixed it. I just check on any select dropdown list with $("select") and from there on I was able to get the selected option.
$("select").change(function () {
var selectValue = $(this).attr("id");
var selectOption = $("#" + selectValue + " option:selected").text();
});
I've created a very basic form that has 2 dropdown lists.
Each list contains the same entries:
<option value='1234:0'>Closed</option>
<option value='4567:2'>Open</option>
<option value='6857:1'>Dead</option>
<option value='9856:1'>Alive</option>
<option value='0000:0'>Other</option>
If an entry in dropdown 1 is selected, then that entry should be removed from dropdown 2
If a different entry is selected in dropdown 1, then that should be removed from dropdown 2 and the original entry returned to dropdown 2
if nothing is selected in dropdown 1, then all options should be shown in dropdown 2.
I've created a FIDDLE, showing how far I've got... it's not very far.
Can any one help with this ?
Thanks
Try this
$(function(){
$('#test').change(function () {
var selected = $(this).val();
$("#test2 option").show();
$("#test2 option[value='" + selected + "']").hide();
});
});
If you want to use text you can try this
$(function(){
$('#test').change(function () {
var selected = $('option:selected', this).text();
$("#test2 option").each(function(){
if($(this).text() == selected) {
$(this).hide();
} else {
$(this).show();
}
})
});
});
Since the value of each the options are identical, you can use value attribute to select the match in second select.
$('form').on('change', 'select[name="test"]', function() {
var selected = $(this).val();
$("#test2 option").show();
$("#test2 option[value='" + selected + "']").hide();
});
Demo
I've looked through stackoverflow for an answer and am almost there but need some help.
I have multiple drop down lists with the same options in them. For example three identical lists with the following:
<label for='distlist_1'>Distribution List 1</label>
<select name='distlist_1'>
<option value=''>Please Select:</option>
<option value='1'>All</option>
<option value='2'>All Managers</option>
<option value='3'>Leavers</option>
</select>
The only difference being the name eg, distlist_1, 2, 3 etc. I can add ids if necessary.
When a user selects an option in the first drop down list I need it to be removed from all other drops downs. I found a script that did this.
$('option').click(function(){
$('option:contains(' + $(this).html() +')').not(this).remove();
});
But I need it so that if the user then decides, 'wait I don't need that option in drop down list 1 after all', she picks something else and the option reappears in the other drop downs. The code above removes the options then there is no way of retrieving them until if you click enough they all disappear.
This actually does what you want... This is based on jquery 1.7 and the on() event binder
$(document).ready(function (){
$('select').on('focus', function() {
// on focus save the current selected element so you
// can place it back with all the other dropdowns
var current_value = $(this).val();
// bind the change event, with removes and adds elements
// to the dropdown lists
$(this).one('change.tmp', { v : current_value }, function(e)
{
if ($(this).val() != '')
{ // do not remove the Please select options
$('option[value="' + $(this).val() + '"]')
.not($('option[value="' + $(this).val() + '"]', $(this)))
.remove();
}
if (e.data.v != '')
{ // do not add the Please select options
$('select').not($(this)).append($('option[value="' + e.data.v + '"]', $(this)).clone());
}
});
// on blur remove all the eventhandlers again, this is needed
// else you don't know what the previously selected item was.
$(this).on('blur.tmp', { v : current_value}, function (e)
{
$(this).off('blur.tmp');
$(this).off('change.tmp');
});
});
});
The only thing it doesn't do is ordering everything in the right order. You will have to think of your own way of doing that at the .append function.
I ended up using this as it was concise...
jQuery(document).ready(function() {
var selects = $('.mapping')
selects.change(function() {
var vals = {};
selects.each(function() {
vals[this.value] = true;
}).get();
selects.not(this).children().not(':selected').not(':first-child').each(function() {
this.disabled = vals[this.value];
});
});
});
I'm cloning input fields on a table that have an autocomplete class. When I clone the fields I have no problem. The problem is that in the cloned fields, the autocomplete doesnt work (on the one that is not cloned it does work). My autocomplete code is this:
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#input' + num).clone().prop('id', 'input' + newNum);
newElem.find(':input').each(function() {
var name = $(this).attr('name').replace(/\d+$/, '');
$(this).prop({id: name + newNum, name: name + newNum}).val("");
});
// insert the new element after the last "duplicatable" input field
$('#input' + num).after(newElem);
// enable the "remove" button
$('#btnDel').prop('disabled','');
// business rule: you can only add 15 names
if (newNum == 15)
$('#btnAdd').prop('disabled','disabled');
});
$('#btnDel').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
$('#input' + num).remove(); // remove the last element
// enable the "add" button
$('#btnAdd').prop('disabled','');
// if only one element remains, disable the "remove" button
if (num-1 == 1)
$('#btnDel').prop('disabled','disabled');
});
$('#btnDel').prop('disabled','disabled');
});
My Autocomplete code is :
var autoc = {
source: "lib/search.php",
minLength: 1,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
};
var renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a style='height:75px; text-align:center; font-weight:bold;'>"+ item.label + "</a>" )
.appendTo( ul );
};
$(".member").each(function (i) {
$(this).autocomplete(autoc).data("autocomplete")._renderItem = renderItem;
});
I've been trying to fix it by putting the autocomplete code inside of the clone code, Im not sure what Im doing wrong. It would be great if somebody could help! Thanks!
You have to reinitialize the autocomplete field after it's been cloned. And I think wrapping it within .live() is necessary as well
My solution to this was something like this:
$('#my_clone_button').live('click',function() {
my_clone_script; #this is my function to clone
$('select your new cloned input').each(function() {
$(this).autocomplete('destroy');
enable_autocomplete($(this), json_url); #this is my function to initialize autocomplete
});
});
try $('#input' + num).clone(true) passing true tells the clone to copy the events and data also. This means that it will copy that the input is a autocomplete field.