jQuery extracting value of a custom attribute in <option> field - php

Okay so for example I have declared a custom value in a select for for example
<select id="something">
<option value="1" data="01"></option>
<option value="2" data="02"></option>
<option value="3" data="03"></option>
<option value="4" data="04"></option>
</select>
For some reason I cannot use the value attribute here. I need to use data attribute to extract the value. I tried using jquery as below:
var dataString = {
clicks: $("#clicks").val($(this).find(':selected').attr('data'))
};
But this is not returning the result as expected. In console its showing too much recursion. I am not getting the value of the data attribute here (01, 02, 03 etc). Please help.

I would recommend you to use data-* prefixed attribute which can be accessed using .data()
console.log($("#something option:selected").data("value"))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="something">
<option value="1" data-value="01"></option>
<option value="2" data-value="02"></option>
<option value="3" data-value="03"></option>
<option value="4" data-value="04"></option>
</select>

Try this code
<select id="selectNo">
<option value="1" data-no="01">1</option>
<option value="2" data-no="02">2</option>
<option value="3" data-no="03">3</option>
<option value="4" data-no="04">4</option>
</select>
And Jquery
$("#selectNo").change(function () {
alert($(this).find(':selected').data("no"));
});
Output

I assume this is your dropdown. Try the following:
$("#clicks").val($(this).find('option:selected').attr('data'));

$('#something').change(function(e) {
//var id = $(this).val();
$('#something :selected').each(function(){
var value = $(this).data('id');
alert(value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="something">
<option value="1" data-id="01"></option>
<option value="2" data-id="02"></option>
<option value="3" data-id="03"></option>
<option value="4" data-id="04"></option>
</select>

Try to use like below.
var option = $("#something option:selected").attr("data-value");
Exp:
$("#something").change(function () {
console.log($("#something option:selected").attr("data-value"))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="something">
<option value="1" data-value="01">01</option>
<option value="2" data-value="02">02</option>
<option value="3" data-value="03">03</option>
<option value="4" data-value="04">04</option>
</select>

Related

How To Get Unselected Option Value In Bootstrap davidstutz/bootstrap-multiselect dro down list

I am using davidstutz/bootstrap-multiselect drop down list and I want to get unselected value(s) when I get a drop down list:
<div class="form-group">
<label for="Course" class="col-sm-4 control-label">Dashbord *</label>
<div class="col-sm-4" id="multicourse">
<select id="Course" name="options[]" multiple="multiple" style="width:100%">
<option value="1" >Sales</option>
<option value="2" >Students</option>
<option value="3" >Quetion</option>
<option value="4" >Test</option>
<option value="5" >Test Series</option>
<option value="6" >Student</option>
<option value="7" >Validity</option>
<option value="8" >Disk Space</option>
<option value="9" >Student Graph</option>
<option value="10" >Date</option>
</select>
</div>
</div>
In the script:
$('#Course').multiselect({
includeSelectAllOption: true,
nonSelectedText:"Select Dashbord",
nSelectedText:"Dashbord",
enableFiltering: true,
maxHeight:200,
buttonWidth: '95%',
});
$("#Course").multiselect('selectAll', false);
$("#Course").multiselect('updateButtonText');
I am not sure whether I understand your questions, but here are some proposals. You can get the values of all selected options as follows:
$('#Course option:selected')
Similarly you can get all unselected options:
$('#Course option:not(:selected)')
The corresponding input elements can be queried as follows. I am assuming the you use buttonContainer: '<div id="multiselect-container"></div>' as option for simplicity:
// Alternatively use `:not(:selected)` as above:
var value = $('#Course option:selected')[0].val()
$input = $('#multiselect-container input[value="# + value + '"]') // The checkbox
$input.prop('checked`) // Whether it is checked (its checked when the correspondign option is selected)
$input.closest('li') // Corresponding list item
// ...

how to create select all option from dropdown?

I am having problem in my search box, actually i have created a search box for multiple selection and it is working properly,
<input type="text" name="search" size=15 maxlength=15 placeholder = "Gene Symbol"/>
<select name="table[]" size = "0" multiple>
<option selected="selected"></option>
<option value="infla_info">Inflammation</option>
<option value="diet_info">diet</option>
<option value="obesity_info">obesity</option>
<option value="stress_info">stress</option>
<option value="athero_info">atherosclerosis</option>
<option value="retino_info">Diabetic Retinopathy</option>
<option value="nephro_info">Diabetic Nephropathy</option>
<option value="neuro_info">Diabetic Neuropathy</option>
</select>
<input type="Submit" name="Submit" value="Gene Search"/>
after this i want to add one more option select all, by which it should show all the records or tables containing query gene.
please help me
try this
Jquery Code:
$('#selectall').click(function() {
$('#countries option').prop('selected', true);
});
Live Demo :
http://jsfiddle.net/3nUPF/177/
You can achieve this using jQuery :
EDIT :- You can select multiple value by pressing Ctrl button of your keyboard and you can get those value using print_r($_POST['countries']);
<select name="countries" id="countries" MULTIPLE size="8">
<option value="all">Select all</option>
<option value="UK">UK</option>
<option value="US">US</option>
<option value="Canada">Canada</option>
<option value="France">France</option>
<option value="India">India</option>
<option value="China">China</option>
</select>
jQuery :
<script>
$(document).ready(function()
{
$('#countries').change(function() {
if($(this).val() == 'all')
{
$('#countries option').prop('selected', true);
}
});
});
</script>
Working Demo

when selecting a value from a dropdown list set other dropdown list to it's default value

I have an php form which contains 7 dropdown lists. When selecting a value from one of this I want the others 6 to return to their default values if they were previously opened. I think it should be used a javascript for this but I don't now to write such a script. I guess each of this dropdown list must have an ID and when I select an option from each one of the 7 the javascript should recognise the id and sets the other to default. I tried using a javascript with document.getElementsByName('id') but it doesn't work.
<form id="form1" name="form1" method="post" action="">
<select name="select" size="1" id="1">
<option value="0" selected="selected">please select</option>
<option value="1">blue</option>
<option value="2">green</option>
<option value="3">red</option>
</select>
<br />
<select name="select2" size="1" id="2">
<option value="0" selected="selected">please select</option>
<option value="1">intel</option>
<option value="2">amd</option>
</select>
<br />
<select name="select3" size="1" id="3">
<option value="0" selected="selected">please select</option>
<option value="1">fish</option>
<option value="2">car</option>
<option value="3">table</option>
</select>
<br />
<select name="select4" size="1" id="4">
<option value="0" selected="selected">please select</option>
<option value="1">lcd</option>
<option value="2">led</option>
</select>
</form>
This is my form.
Let's sey for example I select a value from the second dropdown list "intel". After this i select from the 3rd dropdown list a value "table". What i need to do is when selecting from the 3rd dropdownlist the second one returns to "please select".
The ideea is that using a javascript i do not want those who use the form to select more than one dropdown list at a time.
They should be able to see the values from each one opening the dropdown list but if they open another one the previously must return to the selected="selected" option which is "please select".
Thanks.
Here is a idea to start. Consider 2 dropdown like this.
<select id="Numeric">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="Alphabets">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
Based on the value of 1st dropdown i am selecting the second dropdown
var val = document.getElementById("Numeric").value;
var obj = document.getElementById("Alphabets");
if(val == "1") obj.value = "A"
else if(val == "2") obj.value = "B"
else if(val == "3") obj.value = "C"
Switch is more better
switch(val)
{
case("1"):
obj.value = "A";
break;
case("2"):
obj.value = "B";
break;
case("3"):
obj.value = "C";
break;
}
This is very basic starup idea. You need to implement good logics for your needed functionality
Updated Answer after Comments
<html>
<head>
<script type="text/javascript">
function handleSelect(thisObj){
var list = document.getElementsByTagName("SELECT")
for(var i=0; i< list.length; i++)
{
if(list[i] ==thisObj) continue;
list[i].value = "0";
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<select name="select" size="1" id="1" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">blue</option>
<option value="2">green</option>
<option value="3">red</option>
</select>
<br />
<select name="select2" size="1" id="2" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">intel</option>
<option value="2">amd</option>
</select>
<br />
<select name="select3" size="1" id="3" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">fish</option>
<option value="2">car</option>
<option value="3">table</option>
</select>
<br />
<select name="select4" size="1" id="4" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">lcd</option>
<option value="2">led</option>
</select>
</form>
</body>
</html>

how to get the value of the dropdown jquery

I want to get the value of the dropdown. I am using msDropDown, but i am getting its value as undefined. The html as follows:
<select name="category[]"
id="webmenus_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>"
onchange="showValue(this.value)"
>
<option value="0" selected="selected" title="Please select hotel category"></option>
<option value="5_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/5star.png"></option>
<option value="4_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/4star.png"></option>
<option value="3_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/3star.png"></option>
<option value="2_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/2star.png"></option>
<option value="1_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/1star.png"></option>
</select>
jquery alert($("input[name='category[]']").val()); alerts as undefined.
How can i get the value of the dropdown?
Thanks,
$(":input[name='category[]']").val()
You use select instead of input
Edit your HTML and set the select Tag to mutiple
<select name="test" multiple>
<option value="1">ITEM 1</option>
<option value="2">ITEM 2</option>
<option value="3">ITEM 3</option>
</select>
alert($('select[name="test"]').val());
// outputs 1,2,3 when you select all three options
try
$("class of select ").change(function() {
alert($(this).val());
});

Select all options in select

I have a multiple select. How can make, that all options will be selected always? Or how make a selecting all options ob submit. I have only class of select(not ID).
I think you can even write your replacement line like this:
$(".someClass option").attr("selected", "selected");
to avoid the .each() loop.
Here's a slight modific to Kitsched's code to ensure that you're able to select the button/select control if you only have the class and not the id.
<html id="html">
<body id="body">
<script src="file:\\\D:\Sidharth\javascript\jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
function SelectAll() {
$(".someClass").children().each(function () { $(this).attr("selected", "selected"); });
}
$(".clickme").bind("click", SelectAll);
});
</script>
<input type="submit" class="clickme" />
<select multiple="yes" class="someClass">
<OPTION VALUE="1" id="one">Option 1</OPTION>
<OPTION VALUE="2" id="two">Option 2</OPTION>
<OPTION VALUE="3" id="three">Option 3</OPTION>
<OPTION VALUE="4" id="four">Option 4</OPTION>
</select>
</div>
</body>
</html>
If you've IDs for either button or select just replace the . with # in script. For e.g. if button's id is clickme instead of class just use #clickme instead of .clickme.
Hope this is what you're looking for.
I'm not 100% sure that this is what you're asking for, but here it goes:
<SELECT MULTIPLE="yes" ID="multipleSelect">
<OPTION VALUE="1" SELECTED="selected">Option 1</OPTION>
<OPTION VALUE="2" SELECTED="selected">Option 2</OPTION>
<OPTION VALUE="3" SELECTED="selected">Option 3</OPTION>
<OPTION VALUE="4" SELECTED="selected">Option 4</OPTION>
<OPTION VALUE="5" SELECTED="selected">Option 5</OPTION>
<OPTION VALUE="6" SELECTED="selected">Option 6</OPTION>
</SELECT>
<INPUT TYPE="submit" ID="submit" value="Go!"/>
For the dynamic option (with jQuery):
$("#submit").click(function() {
$("#multipleSelect option").each(function() {
$(this).attr("selected", "selected");
});
});
Untested.
Good luck.

Categories