Selected item in dropdown jquery

Posted: logo-go Date: 25.05.2017

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:. However, I need to do this with jQuery, because I'm using a CSS class for my selector stupid ASP.

jquery - Get selected option from select element - Stack Overflow

When I put an alert just above it it works fine, but when I remove the alert and let it run at full speed, I get the error. I'm not sure if it's a bug with jQuery or Internet Explorer 6 I'm guessing Internet Explorer 6but it's terribly annoying. 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.

Because in my case, I'm using selected option's data attribute instead of value attribute.

javascript - Change the selected value of a drop-down list with jQuery - Stack Overflow

So if you do not have unique value for each option, above method is the shortest and sweet!! These solutions seem to assume that each item in your drop down lists has a val value relating to their position in the drop down list. So Item 4 will show as "Selected" in the browser and now you want to change the value as 3 and show "Item3" as selected instead of Item4. So as per the above solutions,if you use. You will see that Item 3 as selected in browser.

But when you process the data either in php or aspyou will find the selected value as "4". The reason is thatyour html will look like this. I would appreciate comments on this from senior programmers like stragery0mboISIK and others. So I changed it so that now it executes after a miliseconds using setTimeout. Seems to be working now. I have run into this many times when loading data from an Ajax call.

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: A small sample below:. Just a note - I've been using wildcard selectors in jQuery to grab items that are obfuscated by ASP.

NET CLient IDs - this might help you too:. 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.

Then we can use the field we put into dataClsFielddo a find for myValue and make it selected using.

Or, you could just use. Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site the association bonus does not count. How much do the ice road truckers make in a season you like to answer one of these unanswered questions instead?

Stack Overflow Questions Developer Jobs Documentation beta Tags Exchange rate rand to pound. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7. Join them; it only takes a minute: Change the selected value of a woodstock post office trading hours list with jQuery Ask Question. Using regular JavaScript, I would do something like: Here are a few things I've tried: How can I do it with jQuery?

Update So as it turns out, I had it right buying stocks on a margin first time with: Invalid Index I'm not sure if it's a bug with jQuery or Internet Explorer 6 I'm guessing Internet Explorer 6but it's terribly annoying. Ram 2, 9 25 I was creating new option elements for the select element and then trying to set the value to one of those newly created option elements.

IE6 incorrectly waits until it has gotten control back from a script to actually create the new elements in selected item in dropdown jquery DOM so effectively what was happening is I was trying to set adsense beginner make money ideas drop down lists to options that did not exist yet, even though they should have. This behavior is in jQuery versions 1.

You most likely want this: This alone is reason enough to use jQuery.

Does this work if the select is hidden display: I can't get it to work right Nordes that would be the case if getting the selected value. In this case it is setting the selected value, or more accurately, setting the selected option.

You need to add. With hidden field you need to use like this: Aivar Luist 6 2. Could you please explain why do you need to fire change event for hidden fields? If you need a change event fire after changing the value of the dropbox, you need stock market java program call the change function after setting the value.

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: NET will render the control ID correctly inside the jQuery. Also, organization of forex market opening time in pakistan on how deep your controls are nested in your application in my case they were about 10 levels deep the ClientID can get incredibly long and can actually add significant bloat to the size of your javascript if used too liberally.

I try to keep my markup as small as possible, if I can. JS files for your JavaScript so that browsers and proxy servers can cache it for performance. Roberto - but MVC doesn't use the stupid naming conventions that asp. If you create OO-style javascript you can pass your client Ids into the constructor of your object. The object code is all contained in an external js file, but you instantiate it from your aspx code. You have to be careful if you're then putting that control in a repeater though.

After looking at some solutions, this worked for me. I guess this is the shortest, reliable, general and elegant solution. ISIK 1 2. Hurray DOM API and the people who wrote it back in the stone age. Things are a little more complicated if this isn't the case. To read the selected index of a drop down list, you would use this: Let's see how you would use these two functions.

Supposing you had a drop down list of month names. I know this is a old question and the above solutions works fine except in some cases. So what I do is actually ,removed the selected attribute and then make the new one as selected.

selected item in dropdown jquery

Rocker Maruf 1 4. A small sample below: Keith 1, 6 26 I use an extend function to get client ids, like so: NET controls in jQuery like this: Mych 1, 2 16 NET CLient IDs - this might help you too: AVH 7 7. Pervez Choudhury 1, 1 17 But your suggestion is probably faster than only matching on a classname. Even faster though would be element. If we have a dropdown with a title of "Data Classification": This does not provide an answer to the question.

To critique or request clarification from an author, leave a comment below their post. I fail to see how this adds anything beyond what the other 20 answers already address.

Thank you for posting an answer to this question! Code-only answers are discouraged on Stack Overflow, because it can be difficult for the original poster or future readers to understand the logic behind them.

Please, edit your question and include an explanation of your code so that others can benefit from your answer. It shows getting the dropdown field, assigning a variable a value that would be in that dropdown, then doing a find on that value and setting it to selected.

Or, you would just set it using. OP even admitted he used that, but another said to put in '2' instead of just 2, in the parenthesis, so my answer was correct-just no one understood it.

Also, no other answers used. When I was trying to do this, none of the other answers were working for me-had to do it this way. Stack Overflow works best with JavaScript enabled. The problem here ended up being an issue with IE6. That only works when your javascript is inside of the. This is the correct answer, and everyone who did not say 'selectedIndex' instantly, should know better.

I would appreciate comments on this from senior programmers like stragery0mboISIK and others share improve this answer. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system