1

When I load html content using AJAX that includes a selected option, it is not rendered on the client.

For example

<option selected='selected'>text</option>

Now if I use

$("option [selected]")

I get empty result, but it works well in FireFox. I checked source code using Develper Tools and selected is not in the html content. That's weird!

animuson
  • 53,861
  • 28
  • 137
  • 147
  • It should be `` with double quotes – morgar Apr 25 '11 at 00:32
  • try without space `$("option[selected]")` – KJYe.Name Apr 25 '11 at 00:33
  • I think it must be `$("select option:selected")` – morgar Apr 25 '11 at 00:35
  • I tried with "", $("option[selected]") but it does not work. $("select option:selected") behaves differently. When there is an item selected it works fine, but if no option is selected it just returns the first option which is not correct. –  Apr 25 '11 at 00:38
  • Maybe this could help you: http://stackoverflow.com/questions/1318076/jquery-hasattr-checking-to-see-if-there-is-an-attribute-on-an-element – morgar Apr 25 '11 at 00:45
  • Actually, I'm not sure what are you final goal. Knowing if some option is selected or if it has the selected attribute. – morgar Apr 25 '11 at 00:50
  • I have to loop through all options to test each one for .attr("selected"). It shouldn't be like that because i am using a jQuery plugin that already uses jQuery [] syntax. Now whose bug is this? IE? or jQuery? using Developer tools in IE I couldn't see the "selected" attribute rendered Can any of you check on their side? –  Apr 25 '11 at 00:52
  • Here is a test case: http://jsfiddle.net/kR2ks/1/ –  Apr 25 '11 at 01:02

1 Answers1

0

I'm having the same problem, and I think this is an IE8 issue, as it seems to work in IE7...

Have run some tests and I suggest that you try a conditional statement in jQuery and use the square brackets with anything else but IE, and for IE use the colon version:

non IE: $("option[selected]");

IE: $("option:selected");

PS. to be more clear, you can use the colon version for everything but FireFox 3, so FF4 and WebKit will also work with the colon version. Haven't done a multi version thorough test, just assume latest as of this date. So I guess it's up to you if you want to do a conditional based on IE or FF 3...

Victor S
  • 5,098
  • 5
  • 44
  • 62