1

jQuery 1.9.1 or later, Chrome 24 on osx 10.8.2.

I'm having trouble getting Chrome to select a given value in a select list, and that setting obj.val('foo') was not working whereas $("option[value='foo']",obj).prop("selected",true) is working.

I'm getting mixed results between different browsers (ie, chrome, firefox) running on windows and mac. mostly that

 obj.val('foo')

is not currently working on mac chrome only. This is crazy!

I noticed that if I look at the .html() of the object, it does not show which item is selected, even though reading the val() property reports it properly. Which is to say, I'm expecting to (but not) see something like

<option value="bucket" selected="selected">bucket</option>

http://jsfiddle.net/frumbert/a4RzY/

Is this a bug in chrome's implementation of innerHTML() (which http://api.jquery.com/html/ states is being used) ?

Brandan
  • 14,735
  • 3
  • 56
  • 71
frumbert
  • 2,323
  • 5
  • 30
  • 61
  • Change `.prop()` to `.attr()` and it'll work. – Blender Feb 21 '13 at 04:32
  • @Blender prop() is now used in latest versions. – Muhammad Talha Akbar Feb 21 '13 at 04:33
  • @AspiringAqib: `.prop()` doesn't replace `.attr()`. They do different things. – Blender Feb 21 '13 at 04:35
  • @frumbert, `innerHTML` does not reflect the `value` attribute, it's completely irrelevant. – dlock Feb 21 '13 at 04:37
  • well, @Blender yeah! but we can use `removeAttr` before using `prop` ;) if you understand. Well, i use `prop` like that. :D – Muhammad Talha Akbar Feb 21 '13 at 04:38
  • @AspiringAqib: I'm not too sure what you mean by that – Blender Feb 21 '13 at 04:38
  • Read [this question](http://stackoverflow.com/questions/11778123/why-arent-some-technically-serializable-input-properties-serializable) - it is about `input`s, not `select`s but you should get the idea of why some DOM properties are not serializable and that you should be using the DOM instead of reading string representations of it. – Fabrício Matté Feb 21 '13 at 05:04
  • @FabrícioMatté In this exact case I wasn't interested in the string representation of the DOM, however I can think of recent cases where I use `.html()` to store what I normally accept to be the current version of a DOM object, e.g. an entire div's contents. I'll have to watch that next time. I understand (now) the difference between an objects attributes and its properties; the difference that `.innerHTML()` means. Setting something as 'selected' by property isn't the same as setting an attribute that says its selected. Hence the common confusion between `prop` and `attr` I guess. – frumbert Feb 21 '13 at 09:19
  • also, @Blender, tried changing `.prop()` to `.attr()` and it worked in Chrome ... and broke IE – frumbert Feb 21 '13 at 09:21

0 Answers0