Fetch select list value in javascript - javascript

I have the select list in the html page as follows:
<select onchange="storePolishType('/p/','b',this.value );" class="drop_down1">
<option value="12">Margin2–Sawed Margin </option>
<option value="13">Polished-Sawed Back Polished </option>
<option value="13">Polished-Steeled Back Polished </option>
<option value="13">Polished–All Polished </option>
<option selected="selected" value="11">Sawed-All Polished </option>
</select>
I want to pass value e.g. 11 and value between option tag e.g. "Sawed-All Polished" to the storePolishType javascript function. I don't know how to pass this two values.
please help me.

Basically, what you want is:
<select onchange="storePolishType(this.value,this.options[this.selectedIndex].text)" class="drop_down1">
this will pas the value (eg, 11) and the text (eg, "Sawed-All Polished ") to a function storePolishType
live example: http://jsfiddle.net/QFtPG/

Related

Is there any way of making a <select> go to url

I have this code below:
<select name='m-menu' onchange='location = this.options[this.selectedIndex].value;'>
<option selected value='/users/lol'>My Account</option>
<option value='/?signout'>Sign Out</option>
<option value='/photos'>Photos</option>
<option value='/post'>Share/Post</option>
<option value='/feed'>Feed</option>
<option value='/chat'>Create Chat</option>
</select>
However I want it to go to the account if the user clicks on "My Account". Can this be done without adding an other option the the select tag, If so please help???
I will need this to be used for mobile browsers and I need this to go to the url if and when the user selects an option
You can achieve that by changing the selected value when the user focuses on the select onfocus="this.selectedIndex = -1".
<select name='m-menu' onfocus="this.selectedIndex = 0" onchange='location = this.options[this.selectedIndex].value;'>
<option value="" style="display: none">Select an Item</option>
<option selected value='/users/lol'>My Account</option>
<option value='/?signout'>Sign Out</option>
<option value='/photos'>Photos</option>
<option value='/post'>Share/Post</option>
<option value='/feed'>Feed</option>
<option value='/chat'>Create Chat</option>
</select>
I would also suggest you add your event listeners from a separate javascript file instead of using the on* attributes.
You can do that using addEventListener

Angular select and constants: how to select default option?

I've got a select element, like this:
<select id="fieldName"
class="form-control"
ng-model="condition.type">
<option value="{{constants.TYPE_SORT_BY_SENDER}}">
{{locale('Sender')}}
</option>
<option value="{{constants.TYPE_SORT_BY_RECIPIENT}}">
{{locale('Receiver')}}
</option>
<option value="{{constants.TYPE_SORT_BY_DATE}}">
{{locale('Date')}}
</option>
<option value="{{constants.TYPE_SORT_BY_ATTACHMENT}}">
{{locale('Has attachment')}}
</option>
<option value="{{constants.TYPE_SORT_BY_SUBJECT}}">
{{locale('Subject')}}
</option>
</select>
constants object is defined in $rootScope on $stateChangeStart event, so when I look on this element using firebug it looks ok.
But the problem is that this select is not selecting the correct option on page load: if I change a constant to the number it works fine.
I don't want to have any magic numbers in my template, but I can't get this select to work.
What should I do here?

Make an option of jQuery ComboBox not selectable

just like the title says...I have a combobox, whose options are filled with web service call, activated on the click event.
I need to handle that list and make an option of that list (just the one whose value starts with "-" not selectable).
The "-" works a separator between one option group and the second following one.
So let's say I have this combo:
<select id="authority" class="selectauthority" style="width: 300px;" onclick="DisableSpecificOption()" name="authority">
<option value=""> </option>
<option value="001">Federal Bureau </option>
<option value="003">Police </option>
<option value="-1">-</option>
<option value="004">Army </option>
<option value="018">Navy </option>
<option value="018">CIA </option>
<option value="018">NSA </option>
</select>
As far as I know make the option disabled should be enough...I tried several scripts but I wasn't able to obtain my goal.
My Fiddle
Thank you in advance
edit
Since I have to use it in more than one selector and function I wrote a general one.
Updated Fiddle
Hope it helps somebody
Try
$('#authority option[value="-1"]').attr("disabled","disabled")
or
$('#name option:contains("-")').attr("disabled","disabled")

Select of multiselect not working in JavaScript

I have a multiselect option of JQuery:
<select multiple="multiple" id="add_remove_user_segment" size="5" name="add_remove_user_segment[]">
<optgroup label="test">
<option value="test1">
test1
</option>
<option value="test2">
test2
</option>
<option value="test3">
test3
</option>
</optgroup>
<optgroup label="op1">
<option value="op1">
op1
</option>
<option value="op2">
op2
</option>
<option value="op3">
op3
</option>
</optgroup>
</select>
However whatever I select in javascript the following line of code gives me null! Why is that?
alert($("#add_remove_user_segment").val());
If my question is not clear please let me know!
#my_Selelect isn't the ID of your <select>, it's #add_remove_user_segment
Update:
It's returning null because it's running without anything being selected. Here is a fiddle to demonstrate that you need an onchange event to grab the value (or at the very minimum, try grabbing the value ONLY when something is selected)
http://jsfiddle.net/Dtxjv/1/
add_remove_user_segment has no value until you select one.
Your jQuery runs at document load before a value can be selected.
You need to test for on change or add a delay. demo here
http://codepen.io/lpoulter/pen/ukmwy

select box manipulation

I would like to dynamically change the option value of a select tag. Is this possible? When I say value, I mean the "Change Me" portion. If this is possible, can someone please show me how?
<option value="0">Change Me</option>
You can fetch and modify the combobox item's text as follows:
$("#MyCombo option:selected").text()
Note: #shaz; thanks for your comment, my mistake!
Yes it is possible
You will need to look at the options available using a select tag.
<select id='selection'>
<option value='0'>Value 0</option>
<option value='1'>Value 1</option>
<option value='2'>Value 2</option>
<option value='3'>Value 3</option>
<option value='4'>Value 4</option>
<option value='5'>Value 5</option>
</select>
the javascript
var select = document.getElementById('selection');
// to get the currently selected item, use the `selectedIndex` property.
var index = select.selectedIndex; // 3 in case of value 2 selected
// to change its text
select.options[index].innerHTML = 'the new value';
is this what u are looking for?

Categories

Resources