Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 266 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change the selected value of a drop-down list with jQuery

#1
I have a drop-down list with known values. What I'm trying to do is set the drop down list to a particular value that I know exists using **jQuery**.
Using regular **JavaScript**, I would do something like:

ddl = document.getElementById("ID of element goes here");
ddl.value = 2; // 2 being the value I want to set it too.

However, I need to do this with **jQuery**, because I'm using a **CSS** class for my selector (stupid [ASP.NET][1] client ids...).

Here are a few things I've tried:

$("._statusDDL").val(2); // Doesn't find 2 as a value.
$("._statusDDL").children("option").val(2) // Also failed.

> How can I do it with **jQuery**?


----------


**Update**

So as it turns out, I had it right the first time with:

$("._statusDDL").val(2);

When I put an alert just above it works fine, but when I remove the alert and let it run at full speed, I get the error

> Could not set the selected property. Invalid Index

I'm not sure if it's a bug with jQuery or **Internet Explorer 6** (I'm guessing Internet **Explorer 6**), but it's terribly annoying.

[1]:

[To see links please register here]

Reply

#2
Just an FYI, you don't need to use CSS classes to accomplish this.

You can write the following line of code to get the correct control name on the client:

$("#<%= statusDDL.ClientID %>").val("2");

ASP.NET will render the control ID correctly inside the jQuery.
Reply

#3
How are you loading the values into the drop down list or determining which value to select? If you are doing this using Ajax, then the reason you need the delay before the selection occurs could be because the values were not loaded in at the time that the line in question executed. This would also explain why it worked when you put an alert statement on the line before setting the status since the alert action would give enough of a delay for the data to load.

If you are using one of jQuery's Ajax methods, you can specify a callback function and then put `$("._statusDDL").val(2);` into your callback function.

This would be a more reliable way of handling the issue since you could be sure that the method executed when the data was ready, even if it took longer than 300 ms.
Reply

#4
> So I changed it so that now it
> executes after a 300 miliseconds using
> setTimeout. Seems to be working now.

I have run into this many times when loading data from an Ajax call. I too use .NET, and it takes time to get adjusted to the clientId when using the jQuery selector. To correct the problem that you're having and to avoid having to add a `setTimeout` property, you can simply put "`async: false`" in the Ajax call, and it will give the DOM enough time to have the objects back that you are adding to the select. A small sample below:

$.ajax({
type: "POST",
url: document.URL + '/PageList',
data: "{}",
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var pages = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;

$('#locPage' + locId).find('option').remove();

$.each(pages, function () {
$('#locPage' + locId).append(
$('<option></option>').val(this.PageId).html(this.Name)
);
});
}
});
Reply

#5
I use an extend function to get client ids, like so:

$.extend({
clientID: function(id) {
return $("[id$='" + id + "']");
}
});

Then you can call ASP.NET controls in jQuery like this:

$.clientID("_statusDDL")
Reply

#6
Another option is to set the control param ClientID="Static" in .net and then you can access the object in JQuery by the ID you set.
Reply

#7
With hidden field you need to use like this:

$("._statusDDL").val(2);
$("._statusDDL").change();

or

$("._statusDDL").val(2).change();
Reply

#8
After looking at some solutions, this worked for me.

I have one drop-down list with some values and I want to select the same value from another drop-down list... So first I put in a variable the `selectIndex` of my first drop-down.

var indiceDatos = $('#myidddl')[0].selectedIndex;

Then, I select that index on my second drop-down list.

$('#myidddl2')[0].selectedIndex = indiceDatos;

**Note:**

I guess this is the shortest, reliable, general and elegant solution.

Because in my case, I'm using selected option's data attribute instead of value attribute.
So if you do not have unique value for each option, above method is the shortest and sweet!!
Reply

#9
Just try with

$("._statusDDL").val("2");

and not with

$("._statusDDL").val(2);

Reply

#10
<asp:DropDownList id="MyDropDown" runat="server" />

Use `$("select[name$='MyDropDown']").val()`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through