jQuery : How do I put clicked element in input box? - php

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>

Related

How to put link on each month in input( type month)

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>

Get value with multiple elements with common name

I have a problem with map(). I try get all variables from my form but I can't have multiple types of element (input, select, textaera)
<input type="text" name="ctrl[]" />
<select name="ctrl[]" />
<textarea name="ctrl[]" />
var values = $("select[name='ctrl\\[\\]']").map(function(){
return $(this).val();
}).get();
This works fine with input but not for select and textaera. Can you help me?
You have to remove the select from the selector:
var values = $("[name='ctrl[]']").map(function(){return $(this).val();}).get();
Above code will select all type of the input which will have the name ctrl[].
$("select[name='ctrl\\[\\]']") is bounding you to select only select box values with the name ctrl[].
var values = $("[name='ctrl[]']").map(function() {
return $(this).val();
}).get();
console.log(values);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="ctrl[]" value="input_value" /><br>
<select name="ctrl[]">
<option value="selectd_value">option 1</option>
</select><br>
<textarea name="ctrl[]">textarea_value</textarea>

How can I get multiple selectbox values in textbox in the order in which I selected

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>

jquery generating a delimited string, from select

From dropdownlist user can select option and click button to add that option to textbox. At the same time I want to add that that text to hidden field joined each option with "|" deliminator.
<select id="services" multiple>
<option>Mobile</option>
<option>Computer</option>
<option>Electronic</option>
</select>
$( "#services-add" ).click(function() {
//here when this button clicked I want to add options to hidden field(already created) as string with deliminator
}
Here is hidden field that I want to append text
<input type="hidden" name="value" value="" />
Use map to collect all selected data into array and stored to hidden field using attr
$("#services-add").click(function () {
var val = $("#services option:selected").map(function () {
return $(this).text();
}).get();
$("[type=hidden][name=value]").attr("value", val);
});
DEMO
$( "#services-add" ).click(function()
{
var insertText =$("#services option:selected").map(function () {
return this.value;
}).get().join('|');
$('#services-view').val(insertText);
});
Demo:
http://jsfiddle.net/Npm6w/1/

move selected item from one selectbox to another selectbox(with duplicate prevention)

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");
});

Categories