Get value from multiple dropdown list - javascript

I have multiple dropdown menus that are the same, I need them to be the same.
<select name="Obrojgoluba" id="otacmajka" class="selectbox_dodajpostojeceg" onchange="dodajpostojeceg('otac')">
<option value="1">06557-07-681</option>
<option value="2">07216-05-552</option>
</select>
This is my jQuery function to get value from that select list
function dodajpostojeceg(x)
{
var ID = $('select[name="Obrojgoluba"]').val();
alert(ID);
}
But problem is that it gets value from the first select list, not from others I have on my site. When I click on the first one it gets good values. but when I click on other select list on my site that are the same as first one it returns or empty or value it remembered from the first select list

You are using this
var ID = $('select[name="Obrojgoluba"]').val();
And it is possible as you said that there are multiple select on your page. So if they are having same name, above code would select the first one and that is what you are getting.
So you can just change the function and markup to made this code workable.
function dodajpostojeceg(element)
{
var ID = $(element).val();
alert(ID);
}
and call this function as like this
<select name="Obrojgoluba" id="otacmajka" class="selectbox_dodajpostojeceg" onchange="dodajpostojeceg(this)">
JS Fiddle Demo

A strange thing to be doing, but try this:
function dodajpostojeceg(x)
{
var ID = $(this).val();
alert(ID);
}
As CBRoe comments, multiple identical IDs are invalid, and this may hamper your progress...

Make sure that the jquery lib is included and the function is defined before the html
<head>
function dodajpostojeceg(x)
{
var ID = $('select[name="Obrojgoluba"]').val();
alert(ID);
}
</head>
<body>
<select name="Obrojgoluba" id="otacmajka" class="selectbox_dodajpostojeceg" onchange="dodajpostojeceg('otac')">
<option value="1">06557-07-681</option>
<option value="2">07216-05-552</option>
</select>
</body>
Fiddle: http://jsfiddle.net/kR8yH/
When the html is rendered, it will look for a function named dodajpostojeceg() and if it not defined, it will not be able to bind the select and the function
I am assuming that all the selects are uniquely named

Related

Retrieving *.ascx control value in javascript function

My *.ascx control is a combo box, nothing more, it has a few items as such
MyDropDown.ascx
<select id="Properties" name="properties">
<option value="Name">Name</option>
<option value="Age">Age</option>
<option value="Phone">Phone</option>
</select>
MyDropDown.ascx.cs
//inside the class
public string SelectedValue
{
get { return this.SelectedValue; }
set { this.SelectedValue = value; }
}
MyWebPage.aspx
<%# Register TagPrefix="PropertyList" TagName="propertySelector" Src="~/Views/Shared/MyDropDown.ascx" %>
...
<PropertyList:propertySelector runat="server" ID="PropertySelect"/>
...
<script type="text/javascript">
function BtnClick()
{
var selectedValue = $('.PropertySelect').selectedValue;
...
}
how do I access the selected value in the javascript?
First of all, making a C# property isn't going to expose anything to the client.
Your issue is that you need a way to select the select element from the html instead of trying to select your user control. The easiest way to accomplish what you want is by keeping your code the way it is and adding class='propertySelect' to your select (because you're trying to select based on a css class. Your code is confused about whether you should be selecting by ID or by class). Otherwise you need to start thinking of ways to define a dynamic id for the select and write a selector for it.

jquery loop through select tags on option change

I need help looping through all my select tags in my html page and running some functions on each change
<div id="product_options">
<label>Color</label>
<select name="productOptions" id="colorOpt" style="margin-bottom:10px;">
<option value="1">White</option>
<option value="3">Blue</option>
</select>
<label>Size</label>
<select name="productOptions" id="sizeOpt" style="margin-bottom:10px;">
<option value="2">9oz</option>
<option value="4">10oz</option>**strong text**
</select>
</div>
Everything inside of the main div is variable including the label, names and id's
what i want to do is everything someone updates either one of the selected options i need to figure out which select element they chose and get the value along. So for example if they update the color option to blue i need to trigger a call in jquery to show that select element with id colorOpt was updated with the value of 3
Any ideas I am completely lost as to how to do this. Started with this but not sure if its correct or not
$('#product_options').select("select").each(function(selected, index) {
});
It looks like you started thinking that you'd need to loop through every select...but since you said that you need to "figure out which select element they chose" you can rely on the this keyword inside of a change callback.
Fiddle: http://jsfiddle.net/vpqh3kjz/
Code:
// Every time someone updates either selected option fire a callback
$('#product_options select').change(function () {
// `this` represents the element that changed
var id = this.id;
var value = this.value;
// Do something with your data here
console.log('select element id ' + id + ' was updated with ' + value);
});
$('select').on('change',function(){
var value = $(this).val();
alert(value);
});
Just for helping if someone like land on this question like me.
I searched for a script which i can use to append something according to selected value.
here is what i come up with. JSFIDDLE
Appending code on select field value change and then what's in the value can be took for a loop to append that amount of tags in specific container.
Note: This might give you an error if you have options value other then numbers.
// Every time someone updates selected option fire a callback
$('#product_options select').change(function () {
var container = $('#container');
container.empty();
for(var i = 0; i< this.value; i++){
container.append('<p> index: '+ i +" <br> value: "+ this.value +'</p>');
}
});

Basic java script to get combobox

I am just starting out with some java script in an asp.net mvc web site.
I current have a form which I am working on.
The first field which the user is prompted with is a combobox / select (in html)
here is the code for it:
<select name="select">
#foreach (var item in Model.networks)
{
<option value="">#Html.DisplayFor(modelItem => item.name)</option>
}
</select>
Now my next field depends on the option which they chose from the combo box.
How can I populate the next field based on the option they chose in the combo box?
So when the user navigates to the page they will ave a combo box populated with all the options. Below that will be empty fields. When the user selects a option in the combo box I want it to then populate the empty fields with the corresponding data from the option which was chosen.
How do I go about doing this?
Please give the newby answer as in the method in which it will be done. I am assuming that I will be using java script for it?
Although I cannot understand your question in detail, I hope I can help you.
HTML
If you have a select element that looks like this:
<select id=dropdown>
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
Plain Javascript solution
Running this code:
var element = document.getElementByID('dropdown');
var current = e.options[e.selectedIndex].value;
Would make current be 2. If what you actually want is test2, then do this:
var e = document.getElementById('dropdown');
var strUser = e.options[e.selectedIndex].text;
Which would make current be test2
Put the onChange="getSelectedValue" attribute of the select element and then use the following javascript.
<script>
function getSelectedValue(sel)
{
txtToFill.Text(sel.options[sel.selectedIndex].text);
}
</SCRIPT>
If I understand your question correctly you want to react to a combo box value changing and display different content in your page.
If that is the case what you need to know is
how to handle the change event in the select (drop down)
populate empty fields
Here's how you can register and handle the change event in the dropdown:
$("select[name='select']").on("change", function(){
$('#input1').val("What you want in the input goes here");
});
Here's a fiddle that demonstrates this.

jQuery - get the selected options of a listitem collection?

I have fields that a user would enter and i would pass it into jQuery and pass to a web service.
I can get textboxes values by:
var name = $("#MainContent_txtName").val()
The problem I'm having is a multi-select that comprises of a list item collection.
If I was to do this server side, I would do something like:
foreach (ListItem li in listTitles)
{
if (li.Selected)
{
selectedValue += li.Value + ",";
}
}
And this would give me the string of selected values from the select list.
I'm having trouble getting it out from jQuery.
If I do
var titles = $("#MainContent_selectListTitles").val()
That is obviously incorrect because it won't bring back the selected list items.
I saw a post that suggested that I could retrieve it if I say option selected.
So I tried this:
var titles= $('#MainContent_selectListTitles option:selected');
The next thing I did was pop an alert to see what the titles were. It just said [object, object].
So my questions are:
Is it possible to get the selected items from the list item collection concatenated into a string?
Or is it better that I get all the form values from a postback even on my code behind and then call the jquery function? If this is an option, i've attempted to do this but have failed. It tells me that it can't find the method. So i'm definitely not calling the jquery function correctly on postback of the button event.
THanks.
I'm not surprised that you've got [object, object] in your alert - that's because you've had and array of selected items (actually, two items were selected) and alert displayed your array as [object, object] which is fine.
Having the following html markup:
<div>
<select multiple="multiple" id="mySelect">
<option value="test1" selected="selected">Test 1</option>
<option value="test2">Test 2</option>
<option value="test3" selected="selected">Test 3</option>
</select>
</div>
And the following script:
<script type="text/javascript">
$.ready = function () {
var selectedItems = $("#mySelect option:selected");
for (var i = 0; i < selectedItems.length; i++) {
alert("The value is: "+selectedItems[i].value + "; the text is: "+selectedItems[i].text);
}
}
</script>
Will probably solve your problem.
Actually, binding the data manually is a huge pain and I do recommend you to look on the modern MVVM libraries like knockout.js.
For instance:
http://knockoutjs.com/examples/controlTypes.html
First, make sure your item id is correct on client side that is "MainContent_selectListTitles". You can check it by "View Source".
If the ID is correct, follow this.
$('#MainContent_selectListTitles').change(function () {
var str = "";
$('#MainContent_selectListTitles option:selected').each(function () {
str += $(this).text() + " ";
//pass str value to your web service here.
});
}).trigger('change');
Every time there is changes, it will call this function which means it will make your web service busy. You can move the above code to button click event if you want.

Jquery Mulitple select field error

I have multiple select field each with multiple options.
So, by using jquery to get the respective selected options content, I would be able to get the selected option content. However, this work for the first select field. But, does not work for others. Others keep referencing the first select field selected options content, instead of it owns. Please guide me! Thanks!
My Select fields
<select id="resource_dd" name="resource_dd">
<option selected="selected" value="nil1">No resources.</option>
<option value="1">Week1</option>
<option value="2">Resource1</option>
</select>
<select id="module_dd" name="module_dd">
<option selected="selected" value="nil2">No restriction.</option>
<option value="1">IT1234</option>
<option value="2">IT2345</option>
</select>
Jquery to retrieve the selected content.
$("#module_dd").change(function ( event ) {
var option = $("this").children();
var module_id = $(option+":selected").val(); //module_id get 'nil1' as content instead of nil2
});
Just try this, it will give you the selected value.
$("#module_dd").change(function ( event ) {
var module_id = $(this).val();
});
First of all, you should be saying $(this) instead of $("this").
Second, $(event.target) is probably more appropriate here.
Third, using .children as in $(event.target).children(":selected") will get you what you want.
This is happening because you are referencing $("#module_dd").
You need to add another function and change that to $("#resource_dd") to get the other select field.
Problem code
var option = $("this").children();
var module_id = $(option+":selected").val();
You can't concat a selector string to a jQ collection object. You need to filter the option collection for those which are selected either by:
var option = $( this ).children(":selected");
Or
var option = $( this ).children();
var module_id = option.filter(":selected").val();
Please see Eli Courtwright's answer, but in addition to that, you should do something like $("select") or $("#module_dd, #resource_dd") to target both select fields.

Categories

Resources