Add, select, or clear items
4 stars based on
Opened 7 years ago. Closed 7 years ago. Last modified 6 years ago. Depending on the browser, the final behavior is different. In the last version of Firefox 3, the first option is selected. Either way, you're setting a value and the value isn't being set.
With a non-multiple select, jQuery needs to preserve the existing value if it can't set the requested one. I don't think there's any other safe behavior, since. Actually, having reviewed the source, this looks intentional. My guess is we probably don't want this behavior as it's not consistent. After setting the selectedIndex to -1, val then returns set first option selected jquery instead of the default selected option on retrieval for certain browsers at least such as chrome F4 returns the default selected option.
Depending on which direction we want to go whether to return null or the default 1we should probably unify this across browsers. This set first option selected jquery not a jQuery bug, to add a new, previously non-existent value to a select menu, see the following: Not sure you understand what I meant.
It's not about being able to select set first option selected jquery options. It's about passing a value that isn't one of the options. We are the ones setting the selectedIndex to -1 instead of letting nothing happen, which is creating the inconsistency between browsers. I think this is valid. Given that dynamic values are common in setting values of select elements, reopening for now. I'm not sure rwaldron understood the issue here. I'm the reporter of this bug.
Timmywil, your assessment trading account singapore ocbc my set first option selected jquery is correct. I am not trying to add new options set first option selected jquery just the opposite.
I'm trying to set a value without knowing if the value is available. Rather than check ahead of time, I want to check after calling. I'm guessing jQuery uses that same find in the val call. When you attempt to set a select to a value that does not exist, jQuery behaves the same way that it would if no option was selected, which according to html spec, the correct behaviour is to set the selectedIndex to -1; how the browser handles this is spec dependent.
Try this just testing the return from select. Anyway, rwaldron and I have discussed this ticket and there do not seem to be other tickets related to this problem. And rwaldron brings up another good point. This is a browser bug. The current version of webkit does not react correctly to setting selectedIndex to -1, but the issue does not exist in Chromium If there are more tickets about this issue in the future, we can investigate further and possibly provide a patch.
Currently, the same code is being used for multiple and single select elements, which is ideal since it makes the code short. I could not think of a way to fix without calling val again or differentiating between single and multiple, which does not seem worth it.
The user might as well validate the entry. If someone would like to present a patch that has no perf loss and does not inflate the set first option selected jquery for selects, I think we would welcome it.
In this situation I need the selects to always have a selection, which WebKit isn't doing. I also need the selected option's text to display elsewhere. Instead of checking for WebKit's behavior every time, I added a simple function. It only changes the selection if the new value exists, and returns the selected object's DOM object so I can access its value and text without additional jQuery calls.
In both Safari and Firefox, adding the selected attribute to an option automatically causes attribute removal on the prior selected option. Set first option selected jquery not sure if it's smart to count on this, but it works for me right now and I have a very small user base.
Opened 7 years ago Closed 7 years ago Set first option selected jquery modified 6 years ago. Description If you start with: Oldest first Newest first Threaded. Show comments Show set first option selected jquery changes. I think you meant rwaldron. That's exactly what I said. That's why I said you were correct! Woops, I read "incorrect". Thanks for the explanation. I did suspect that this is a WebKit oddity. See TracTickets for help on using tickets.
Download in other formats: