I have a select Box for selecting country code :-
<div class="selectWrap">
<select name="countryCode" id="countryCode"></select>
</div>
It gets populated using javascript and gets options like follows :-
<option data-phone-code="+91">91 (India)</option>
Now I want to select the value only +91.
I was trying this
var country = document.getElementById("countryCode");
var prefix = country.options[country.selectedIndex].value;
But this returns the Full value with Country.
How can I achieve this ?
Use getAttribute, for example:
country.options[country.selectedIndex].getAttribute('data-phone-code');
Related
I have a Dropdown Select like this and would like to retrieve the Text of the selected option using D3.js
<select id = "dropdown">
<option value = "Activity1">Watch TV< /option>
<option value = "Activity2">Sleep</option>
<option value = "Activity3">Study</option>
</select>"
I can get the value of my selection by using :
var ActivityNumber = d3.select("#dropdown").property("value") ;
However i am am needing needing help to retrieve the text of the selection.
- Either "Watching TV" or "Sleep" or "Study" based on the browser selection..
Thank you in advance..
The CSS selector you need is 'option:checked'.
So to get the currently selected value using D3 with your markup you would use the following code:
const selectedText = d3.select('#dropdown option:checked').text();
I have a multiple select list. on select for an option from the list,I am calling a javascript function. I want to pass the current selected option object to this javascript function.
How can I do this? I tried following but its not working.
<select onClick="callJavascriptFun(this.option);" >
</select>
for getting the selected option, try the following code:
<select onchange="callJavascriptFun(this.options[this.selectedIndex]);" >
</select>
inside the callJavascriptFun function you will get of course the selected option.
Make it easier with event handler:
html part
<select class="list">
</select>
js part:
$('select.list').change(function(){
var selectedOption = $('option:selected', $(this)); // your selected option object(element)
var current = $(this).val(); // your selected option value
....
});
I am looking to duplicate the value of a select input into a second input field using Javascript. I tried to do this using an onchange event call. What should the js code be in order to populate the input field id ='sort' upon change in the select function?
Here is my code
<input id = "sort">
<select name="sort_select" onchange="document.getElementById('sort').value = ('#sort_select').val();">
<option value="Ascending"> Ascending</option>
<option value="Descending"> Descending</option>
</select>
This does not work. I see the error lies in value assignment (('#sort_select').val();)What is the correct way to do this?
JS solution: FIDDLE
change this:
<select name="sort_select" onchange="document.getElementById('sort').value = ('#sort_select').val();">
to
<select name="sort_select" onchange="document.getElementById('sort').value = this.value;">
Use this code
$("select").change(function(){
$("#sort").val($(this).val());
});
Fiddle
http://jsfiddle.net/5S7mh/
onChange="document.getElementById('sort').value = (this.options[this.selectedIndex]).value;"
Don't use that big ugly line as inline JS though.
Also, start with populating the input box.
<select id="kamal">
<option value"ACTIVE">a<option>
<option value"DISABLED">b<option>
<option value"DELETED">c<option>
</select>
I want to get the value displayed on the page..not the value shown in the option tag
I am interested in "aktiv" not "ACTIVE"
when i write document.getElementById("kamal").value;then the value that is select comes in the variable. But I want the displayed value.
Please help me how can I take this value.
NOTE: By using all the options given below, it will give me the value of the selected option, I want the label of the selected option. I mean the displayed value on html page.
The solution you are looking for is:
To get the value:
var element = document.getElementById("kamal");
var selectedValue = element.options[element.selectedIndex].value;
To get the text:
var element = document.getElementById("kamal");
var selectedValue = element.options[element.selectedIndex].text;
EDIT:
working example at:
http://jsfiddle.net/n85tW/6/
Try :
var sel = document.getElementById("kamal")
alert(sel.options[sel.selectedIndex].value);
Working example here
Note: your <option> tags should be closed with </option>
var element = document.getElementById("kamal");
var selectedValue = element.options[element.selectedIndex].innerHTML;
THis was the solution of my question.
I am trying to build a form that fills in a person's order.
<form name="food_select">
<select name="maincourse" onchange="firstStep(this)">
<option>- select -</option>
<option text="breakfast">breakfast</option>
<option text="lunch">lunch</option>
<option text="dinner">dinner</option>
</select>
</form>
What I'm trying to do is send in the select object, pull the name and the text/value from the option menu AND the data in the option tag.
function firstStep(element) {
//Grab information from input element object
/and place them in local variables
var select_name = element.name;
var option_value = element.value;
}
I can get the name and the option value, but I can't seem to get the text="" or the value="" from the select object. I only need the text/value from the option menu the user selected. I know I can place them in an array, but that doesn't help
var option_user_selection = element.options[ whatever they select ].text
I also need to use the passed in select reference as that is how it's set up in the rest of my code.
Later on, that text/value is going to be used to pull the XML document that will populate the next select form dynamically.
You can use:
var option_user_selection = element.options[ element.selectedIndex ].text
In jquery you could try this $("#select_id>option:selected").text()
var option_user_selection = document.getElementById("maincourse").options[document.getElementById("maincourse").selectedIndex ].text
form.MySelect.options[form.MySelect.selectedIndex].value