I have a select box which is created with PHP like:
echo " <option value=\"".$row['CatId']."\">".$row['CatNaam']."</option>";
Where catid is the id which needs to be sent.
Under the select box I have a jQuery clickable div like:
<div id="addtag" style="cursor: pointer;" onclick="clicktag();">
In my script I have a function clicktag like:
function clicktag()
{
$('#addtag').hide();
alert("Tag added ");
}
</script>
What I want to do is alert the selected value(the catid) so the function must do like:
function clicktag()
{
$('#addtag').hide();
alert("Tag added "+tagid);
}
</script>
But how can I get tagid? I tried a lot of examples but can't get it working.
You need a reference to the <select> element in order to get its value. How you get that kind of depends on how the HTML looks, but the easiest way is to give it an ID (if it doesn't have one already) and select it using that:
<select name="yourSelect" id="yourSelect">
// option tags here
</select>
Then the code:
function clicktag() {
var tagId = document.getElementById('yourSelect').value;
// the rest of your code
}
try val()
function clicktag()
{
$('#addtag').hide();
alert("Tag added "+ $('#yourSelectID').val());
}
Use the change event for the select box:
//where #selectBox is id of your select box
$('#selectBox').change(function() {
var currentValue = $('#selectBox :selected').val();
alert(selectVal);
});
Use .val() to get the <select> element's current value.
Example:
<select name="mySelectElement" id="selectElement">
<option value="0">First element</option>
<option value="1">Second element</option>
...
</select>
<script type="text/javascript">
function onClick() {
alert('The selected value is ' + $('#selectElement').val());
}
</script>
$('#selectBox').live('change',function(){
var currentValue = $('#selectBox :selected').val();
alert(selectVal);
});
The selected value is the catid you say
Put it in a var and it will works.
you can try to put this in your code
function clicktag()
{
$('#addtag').hide();
var currentValue = $('#selectBox :selected').val();
alert(selectVal);
}
</script>
Related
Is there a way to post both a select options value and it's content:
<option value="post this">post this too</option>
Not through HTML alone. You could do it with JQuery and some Ajax.
Example:
var optionValue = $("#yourSelectId").val();
var optionText = $("#yourSelectId option[value='" + optionValue + "']").text();
You can do it only with Javascript
JQUERY Example:
<form id="myform">
<select><option value="post this">post this too</option></select>
</form>
$("#myform").submit(function (){
$("#myform select option).each(function (element){
$(element).val($(element).val() + '||' + $(element).text());
});
});
For the content, you can post with PHP. For the value, I recommend this JavaScript:
function getValue()
{
var value = document.getElementById("optionid").value;
return value;
}
You can add a hidden input which will be populated with selected text from select control, for this you can use for example onchange event to which you will attach a javascript method.
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 still trying to learn jquery so bear with me. I have a dual select box that only works if I select all the results of the second select box after I move them there. What I want is when the first box transfers values to the second second select box, it doesn't require highlighting the options, but posts that second select box on form submit. Here is what
I have:
HTML:
<span id="dualselect1" class="dualselect">
<select name="select1[]" multiple="multiple" size="10">
<?php
$c='0';
foreach($lp_name as $lpn){
echo '<option value="'.$lp_id[$c].'">'.$lpn.' ('.$lp_url[$c].')</option>';
$c++;
}
?>
</select>
<span class="ds_arrow">
<span class="arrow ds_prev">«</span>
<span class="arrow ds_next">»</span>
</span>
<select name="select2[]" multiple="multiple" size="10">
<option value=""></option>
</select>
</span>
JQUERY:
<script type="text/javascript">
jQuery(document).ready(function(){
var db = jQuery('#dualselect1').find('.ds_arrow .arrow'); //get arrows of dual select
var sel1 = jQuery('#dualselect1 select:first-child'); //get first select element
var sel2 = jQuery('#dualselect1 select:last-child'); //get second select element
sel2.empty(); //empty it first from dom.
db.click(function(){
var t = (jQuery(this).hasClass('ds_prev'))? 0 : 1; // 0 if arrow prev otherwise arrow next
if(t) {
sel1.find('option').each(function(){
if(jQuery(this).is(':selected')) {
jQuery(this).attr('selected',false);
var op = sel2.find('option:first-child');
sel2.append(jQuery(this));
}
});
} else {
sel2.find('option').each(function(){
if(jQuery(this).is(':selected')) {
jQuery(this).attr('selected',false);
sel1.append(jQuery(this));
}
});
}
});
});
PHP:
if(isset($_POST['submit'])) {
var_dump($_POST['select2']);
}
Like I said, I have this sort of working. But, if I send a value to select2, I have to highlight it before I submit or else it wont POST. Any ideas?
I've come across this before and you have a couple of options. Using JS you can either push all of the values in the second box into a hidden field as well, or also using JS you can select all of the values in the second box as an onsubmit handler on the form.
I've actually done the latter before, and it works just fine.
Ultimately, a select box (multi or single select) only sends the values that are selected -- so that's why it only works if you select them first. It works a lot like checkboxes do, where the unchecked values just don't get posted.
This should "select" all of them:
$('#myform').submit(function() {
var sel2 = $('#dualselect1 select:last-child');
sel2.find('option').each(function(){
$(this).attr('selected',true);
});
});
OR this would put them into a series of hidden fields:
$('#myform').submit(function() {
var sel2 = $('#dualselect1 select:last-child');
sel2.find('option').each(function(){
var hidden = $('<input type="hidden" name="selectedOptions[]"/>');
hidden.val($(this).val());
sel2.after(hidden);
});
});
and then in PHP you'd get these values by using $_POST['selectedOptions'];
You can simply modify this line jQuery(this).attr('selected',false); in sel1.find....block
with jQuery(this).attr('selected',true); .
In this mode al selection moved from first to second box is automatically selected,
so when you submit form, you directly pass this value.
Try it.
this should work:
if(t) {
sel1.find('option').each(function(){
if(jQuery(this).is(':selected')) {
jQuery(this).attr('selected',true);
var op = sel2.find('option:first-child');
sel2.append(jQuery(this));
}
});
}
Hi guys i have a combobox with jquery - but i cant make the second one populated when the first select coming selected alredy. im try to get the value without change the select.
<form>
<select name="tipo" id="Tipo_Id" class="buscaTiposVeiculos">
<option value="1" selected="selected">teste1</option>
<option value="2">teste2</option>
<option value="3">teste3</option>
<option value="4">teste4</option>
</select>
<select name="marca" class="recebeMarcas">
<option value="">--- Select ---</option>
</select>
</form>
jquery
$('select.buscaTiposVeiculos').change(function () {
$("select.recebeMarcas").html('<option value="">Carregando...</option>');
// var opt = $("select.buscaTiposVeiculos"); tried like this
// var val = $(this).val(); tried like this
// var val = $(this).find('option:selected').val(); not works
$('select.recebeMarcas >option').remove();
$.post('/inc/geraCidades.php', {
tipov: $(this).val(),
tipo: "tipo"
}, function (data) {
$('select.recebeMarcas').html('<option value="">Selecione a Marca</option>' + data);
});
});
your selected value is
$("#Tipo_Id option:selected").val();
To trigger change event on page load and populate second select automatically. Some commented code can be removed
/* create the change handler use ID is better selector for efficiancy*/
$('#Tipo_Id').change(function() {
/* this populates second select with one option*/
$("select.recebeMarcas").html('<option value="">Carregando...</option>');
/* this removes option populated in line above*/
$('select.recebeMarcas >option').remove();
$.post('/inc/geraCidades.php', {tipov: $(this).val(), tipo : "tipo"}, function(data) {
$('select.recebeMarcas').html('<option value="">Selecione a Marca</option>'+data);
});
/* trigger the change event on page load*/
}).change();
Hi I'm performing the following query on a database which is getting me a dropdown menu. I want this to query the database onchange so that I can use that information in another form.
$dropdown_sql="SELECT product_id, product_name, unit_price, unit_quantity, in_stock FROM products";
$dropdown_result=mysql_query($dropdown_sql);
$options="";
while ($row=mysql_fetch_array($dropdown_result)) {
$id=$row["product_id"];
$product_name=$row["product_name"];
$unit_price=$row["unit_price"];
$unit_quantity=$row["unit_quantity"];
$in_stock=$row["in_stock"];
$options.="<OPTION VALUE=\"$id\">$product_name - $unit_quantity";
}
?>
The dropdown menu:
<SELECT NAME=product_name onchange="dropdown_change()">
<OPTION VALUE=0>Select a food
<?php echo $options ?>
</SELECT>
I can detect the change in javascript
function dropdown_change()
{
alert("hello")
}
BUT I can't get the information from that point, and the I can't put it in the form. Please help.
Add this in select dropdown.
change the select form with:
<select name="product_name" onchange="dropdown_change(this)">
...................
</select>
<script type="text/javascript">
function dropdown_change(el){
var elValue = el.value;
}
</script>
Test: JSFiddle
If you are using jquery library,
function dropdown_change(){
var val = $("select[name=product_name]").val();
$.ajax({
data : {key:val},
url : 'your_url',
success : function(msg){
// do what ever you want
}
})
}