i have two select box, now what i want is
i want to move option from one select box to another via a button
below is my code:
php
<table>
<tr>
<td>
<select size="5" id="suppliedMovies" name="suppliedMovies">
<option selected="selected" value="">Select Movie</option>
<option value="1">sholay</option>
<option value="3">Jism</option>
<option value="4">Rog</option>
<option value="5">Zeher</option>
<option value="6">Awarpan</option>
<option value="7">Paap</option>
<option value="8">paanch<option>
<option value="9">no entry</option>
</select>
</td>
<td>
<input type="button" id="toRight" value=">>"><br>
<input type="button" id="toLeft" value="<<">
</td>
<td>
<select size="5" id="accquiredMovies" name="accquiredMovies">
<option> No item right now</option>
</select>
</td>
</tr>
</table>
Jquery
jQuery(document).ready( function() {
function displayVals() {
var myValues = jQuery("#suppliedMovies").val();
return myValues;
}
jQuery('#toRight').click(function(){
var x=displayVals();
console.log(x);
var txt=jQuery("#suppliedMovies option[value='"+x+"']").text();
console.log(txt);
if(x!=''){
jQuery('#accquiredMovies').append("<option value='"+x+"' >"+txt+"</option>");
}
});
});
i m using above jQuery, that is working fine but, i want that
when one item is copy from one select box to another select box, then that item should be disable(or probably delete) from first select box (to prevent duplicate entry).
i also want to move item from right to left select box
please suggest me optimized jQuery .
Thanks.
UPDATE
if i want to use multiple select box on both side? then how do i use that?
more update
if i click on a item on rightselectbox and move it to left selectbox,
and go further(post) then on right selectbox, there is nothing selected items??
what i need is on right selectbox, there all items shoud be always selected ,
otherwise what i need to do? after i move an item from right to left ,i again have to select rest of items on right selectbox and go further
solution for my update part
i used below code, please take a look and suggest me if somebody finds any mistake/error in this. Thanking You
jQuery('form').submit(function() {
jQuery('#accquiredMovies option').each(function(i) {
jQuery(this).attr("selected", "selected");
});
});
You could use jQuery remove. Like:
$('#suppliedMovies option[value=' + x ']').remove()
..fredrik
EDIT:
You could optimize the function like this:
jQuery(document).ready( function() {
# Store the jQuery object return by the selector so that you don't need to
# make a new selector request next time a user press the #toRight
var suppliedSelect = jQuery('#suppliedMovies');
jQuery('#toRight').click(function () {
suppliedSelect.find(':selected').each(function (index, elem) {
var selectElem = $(elem);
if (selectElem.val()) {
selectElem.val.appendTo('#accquiredMovies');
}
});
});
});
function SelectMoveRows(SS1,SS2)
{
var SelID='';
var SelText='';
// Move rows from SS1 to SS2 from bottom to top
for (var i=SS1.children().length - 1; i>=0; i--)
{
if (SS1.children()[i].selected == true)
{
SelID=SS1.children()[i].value;
SelText=SS1.children()[i].text;
SS2.append($("<option/>", {value: SelID, text: SelText}));
$(SS1.children()[i]).remove();
}
}
SelectSort(SS2);
}
function SelectSort(SelList)
{
// alert("in secod "+(SelList.children().length-1))
var ID='';
var Text='';
for (var x=0; x < SelList.children().length-1; x++)
{
// alert(SelList.children()[x].text)
for (var y=x + 1; y < SelList.children().length; y++)
{
if ($(SelList.children()[x]).val() > $(SelList.children()[y]).val())
{
// Swap rows
alert("x "+SelList.children()[x].value +" y = "+SelList.children()[y].value)
ID=$(SelList.children()[x]).val();
Text=$(SelList.children()[x]).text();
$(SelList.children()[x]).val($(SelList.children()[y]).val());
$(SelList.children()[x]).text($(SelList.children()[y]).text());
$(SelList.children()[y]).val(ID);
$(SelList.children()[y]).text(Text);
// SelList.children()[x].text= SelList.children()[y].text;
// SelList.children()[y].value=ID;
// SelList.children()[y].text=Text;
}
}
}
}
This is also working for moving items from one multi select box to another without duplicating. I wana know how to make order of values same while moving items from one to another select box.
Here's a good example:
HTML:
<form>
<select id="t1available" size=10 multiple><option>Project 1</option><option>Project 2</option><option>Project 4</option></select>
<br />
<input id="t1add" type="button" value=">" />
<input id="t1addAll" type="button" value=">>" />
<input id="t1removeAll" type="button" value="<<" />
<input id="t1remove" type="button" value="<" />
<br />
<select id="t1published" size=10 multiple><option>Project 3</option></select>
</form>
Jquery:
<script type="text/javascript">
function moveSelectedItems(source, destination){
var selected = $(source+' option:selected').remove();
var sorted = $.makeArray($(destination+' option').add(selected)).sort(function(a,b){
return $(a).text() > $(b).text() ? 1:-1;
});
$(destination).empty().append(sorted);
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#t1add').click(function(){
moveSelectedItems('#t1available', '#t1published');
});
$('#t1addAll').click(function(){
$('#t1available option').attr('selected', 'true');
moveSelectedItems('#t1available', '#t1published');
});
$('#t1remove').click(function(){
moveSelectedItems('#t1published', '#t1available');
});
$('#t1removeAll').click(function(){
$('#t1published option').attr('selected', 'true');
moveSelectedItems('#t1published', '#t1available');
});
});
</script>
This example is entirely from the following article, which, unfortunately for English speakers, is in German: Move selected items between checkboxes.
We can do like this:
$('div.buttons').on('click','#add',function(e){
e.preventDefault();
$("#sourceid option:selected").appendTo("#destinationid");
});
Related
I am trying to do something in php when a month is selected in
<input type="month">
I managed to do it with select as following ->
<select name="forma" onchange="location = this.value;">
<?php
foreach($allusers as $usersx) {
$idus = $usersx['id']; ?>
<option value="<?php echo $_SERVER['PHP_SELF']."?ufil=$idus" ?>"><?=$usersx['firstname']?> <?=$usersx['lastname']?></option>
<?php } ?>
</select>
My question is, is there any way to do the same thing with input month without actualy having to confirm it with some kind of button.
Actualy figured it out, for anyone who need to use it here:
<input type="month" id="month" >
<script>
$(function() {
// bind change event to select
$('#month').on('change', function() {
var url = $(this).val(); // get selected value
if (url) { // require a URL
window.location = "filesall.php?date="+url; // redirect
}
return false;
});
});
</script>
Now I get the values in my textbox but not in the right order. This is my code. Please help.
$("#Member_qualification").change(function() {
var selMulti = [];
$.each($("#Member_qualification option:selected"), function(){
selMulti.push($(this).text());
});
$("#Member_print").val(selMulti.join(","));
});
Regarding your question
selectbox values in textbox in the order in which I selected
Makes confusion is it the order in which you are selected sequentially..?
option1then option2 even if option2 is clicked first
if this was the case your code is absolutely correct and check this fiddle http://jsfiddle.net/tintucraju/sr20dp3z/.
or based on the order in which you are clicked
option3 then option1
then
<select multiple id='Member_qualification'>
<option>option1</option>
<option>option2</option>
<option>option3</option>
<option>option4</option>
</select>
Script
var selMulti = [];
$("#Member_qualification option").click(function(e) {
if(!e.ctrlKey) selMulti = [];
var thisOption = $(this).text();
if(jQuery.inArray(thisOption, selMulti)==-1)
selMulti.push(thisOption);
else
selMulti = jQuery.grep(selMulti, function(value) {
return value != thisOption;
});
$("#Member_print").val(selMulti.join(","));
});
Fiddle : http://jsfiddle.net/tintucraju/q5u1h7c5/
Anyway you need to select with ctrl key for multiple selection.
$('select').change(function(){
var selected = $(this).find(':selected').map(function(){
return $(this).text();
});
$('#result').html(selected.get().join(','));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<hr />
<div id="result"></div>
I want to create two select boxes: One that gets it's options from a database and the other that gets it's options depending on the value of the first select box.
My current code is below (I got the value from the first box with an alert, but don't know how to get it in the sql query for the second box). My document name is tutorial.php and I'm not using any other files except for the database functions, which are in include/config.php.
I've followed dozens of tutorials and stack overflow answers, but I can't get it to work. How can I get the select values to the php code on the same page?
jquery:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="js/script.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$( "#shoot" ).change(function(){
id_firstSelect = $("#shoot").val();
loadSecondSelect(id_firstSelect);
});
function loadSecondSelect(first_id)
{
$("#model").ready(function(e)
{
$.get(
route.php, //Filter your select
params, // In this case your id
function(result)
{
$("#model").empty();
$("#model").append('<option value="0">-- Select --</option>');
if(result.response['id_second'].length) // this receive your data
{
for(var i=0, len=result.response['id_2'].length; i<len; i++)
{
$("#model").append('<option value="' + result.response['id_2'][i] + '">' + result.response['name_2'][i]+'</option>');
}
}
},
"json");
});
}
});
</script>
form with php functions:
<form action="" method="POST" enctype="multipart/form-data">
<div>
<select name="category">
<option value="paard" selected>Paarden</option>
<option value="hond">Honden</option>
<option value="mens">Mensen</option>
<option value="eigen">Eigen werk</option>
</select>
<input type="file" name="files[]" multiple id="file"/><p>
Ophalen uit database shoots:
<select name="shoot" id="shoot">
<?php
$values = mysql_query("SELECT distinct name FROM shoots") or die(mysql_error());
//$numrows = mysql_num_rows($values);
while ($result=mysql_fetch_array($values)){
echo "<option value='".$result['name']."'>".$result['name']."</option>";
}
?>
</select><p>
<select name="model" id="model"></select>
<label class="radio">Portfoliomateriaal</label>
<input type="radio" name="folio" value="TRUE" /> <span>Ja</span>
<input type="radio" name="folio" value="FALSE" checked /> <span>Nee</span><p>
<input type="submit" value="Upload" id="submit" />
</div>
</form>
In the first select, you could call a function with the id of the select to filtrate the data of the second select like:
in the first select you could do this to fill the second with the first id:
$( "#id_firstSelect" ).change(function()
{
id_firstSelect = $("#id_firstSelect").val();
loadSecondSelect(id_firstSelect);
}
and call the function to load the second select
function loadSecondSelect(first_id)
{
$("#id_secondSelect").ready(function(e)
{
$.get(
route.php, //Filter your select
params, // In this case your id
function(result)
{
$("#id_secondSelect").empty();
$("#id_secondSelect").append('<option value="0">-- Select --</option>');
if(result.response['id_second'].length) // this receive your data
{
for(var i=0, len=result.response['id_2'].length; i<len; i++)
{
$("#id_secondSelect").append('<option value="' + result.response['id_2'][i] + '">' + result.response['name_2'][i]+'</option>');
}
}
},
"json");
});
}
I have a HTML form with two select elements(listboxes) and two buttons to move the data from one listbox to another(using JavaScript):
<form action="page.php" method="post">
<select name="select1" multiple="yes">
<option value="1">Left1</option>
<option value="2">Left2</option>
<option value="3">Left3</option>
</select>
<input type="button" value="-->" onclick="moveOptions(this.form.select1, this.form.select2);" /><br />
<input type="button" value="<--" onclick="moveOptions(this.form.select2, this.form.select1);" />
<select name="select2" multiple="yes">
<option value="4">Right1</option>
<option value="5">Right2</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
When I hit the Submit button I want to get all the values stored in the listboxes.
foreach ($_POST['select1'] as $value)
{
//save data to database
}
This just gets one selected value(if there was one).
I managed to get multiple values (if they are selected) by putting a [] after the name of the select.
<select name="select1[]" multiple="yes">
But this still doesn't gets the unselected values, and this way the data moving JavaScript function doesn't works either.
Before the form is submitted just select all the options:
document.getElementsByName('submit')[0].onclick = function () {
var s1 = document.getElementsByName('select1')[0];
for(var i=0; i < s1.options.length; i++){
s1.options[i].selected = true;
}
};
var select1 = document.getElementById('select1');
var values = new Array();
for(var i=0; i < select1.options.length; i++){
values.push(select1.options[i].value);
}
To pass the array from page to page, go here
I am not sure why you want to do this but you can call this Jquery code before submitting the form
and add the array value to a hidden input to get to be posted
$("#Submit_button").click ( function() {
var arr = new Array;
$("#select1 option").each ( function() {
arr.push ( $(this).val() );
});
$(#hidden_input).val(arr);
});
I'm trying to get a list going.
I have an empty input box in which I want to store a list: Mary, Kate, Brock
Now I have a select list with those three names in it. I want to know how can I transfer the name clicked from the select list into the input box. If I click a second name, add a comma to separate the one from the previous one.
Appreciate your help. I have no idea to where/how to even start this :(
How about:
<input type="text" id="name" name="name" />
<select name="names" id="names">
<option value="Mary">Mary</option>
<option value="Kate">Kate</option>
<option value="Brock">Brock</option>
</select>
<script>
$(document).ready(function() {
$("#names").change(function() {
name = $(this).val();
current = $("#name").val();
if (!$("#name").val()) {
$("#name").val(name);
} else {
if (current.indexOf(name) == -1) {
$("#name").val(current + ', ' + name);
}
}
});
});
</script>
Adds new name to end of list, only adds a comma when there is more than one name and duplicates are now allowed.
Well you can get the value from an input element with $('#the_id').val();, and you can manipulate the value of an input element in the same way: $('#the_id').val('new value');. Combine that with the .click handler and you should be all set:
$('#selector').click(function(){
var $names = $('#names')
$names.val($names.val() + ', ' + $(this).val());
});
Try use change event:
$('#listbox').change(function(){
var selectedName = $('#listbox option[value='+$(this).val()+']').text();
$('#input-for-insert').append(selectedName);
});
I think you make yourself comma separation)
<input type="text" id="name" name="name" />
<select name="names" id="names">
<option value="Mary">Mary</option>
<option value="Kate">Kate</option>
<option value="Brock">Brock</option>
</select>
<script>
$(document).ready(function()
{
// When the names selection changes
$("#names").change(function()
{
// Get the new name from this selection list
newName = $(this).val();
// Get any names currently in the input box
currentName = $("#name").val();
// Change the value of the name box by adding the newName followed by a comma
// followed by the current names
$("#name").val(newName + ", " + currentName);
});
}
</script>