1

In my project I need to update a backbone model with both the selected value and text from a <select>.

For this purpose I am calling

model.set 'value', $('select').val()
model.set 'value_text', $('select option:selected').text()

(I am using coffee script as well). Because of some problem in jQuery v2.0.3 which I am currently using I am receiving this warning:

Attr.specified is deprecated. Its value is always true.

I know there were questions on SO about this warning, but I want to ask something completely different:

Since updating to a newer version of jQuery (where the problem might be fixed) is not possible in next few months I would like to ask whether there is other way round to receive the selected option's text instead of that used above. I am not against pure JS solution if there is no other using jQuery.

Any help is highly appreciated.

EDIT for @KevinB: The warning is caused by asking whether there is selected attribute on that option.

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • Did you try option:active? What pseudo selectors have you tried? – fauverism Jul 02 '14 at 14:37
  • 2
    What exactly causes the warning? the use of :selected? or the use of .text on an option? .val() on a select?. Cutting out the part that causes it would obviously be the solution. That, or simply ignore the warning because coding around it is going to make the code less readable. – Kevin B Jul 02 '14 at 14:37
  • Can you create jsfiddle for this? – Artem Volkhin Jul 02 '14 at 14:39
  • Also, apparently this doesn't affect jquery 1.10.1. (haven't tested this myself) – Kevin B Jul 02 '14 at 14:43
  • @KevinB Updated question... Simple googling for that warning could give you better results (what is causing it), but I understand that clicking on *Close > Unclear what are you asking* is so damn simple. Anyway, the question is clear enough - but instead of answering the question you just have to stir the mud around the warning... I have also mentioned that upgrading the jQuery is not possible now... And going from 2.0.3 to 1.10.1 does not look like upgrade at all... – shadyyx Jul 02 '14 at 15:06
  • because the warning is very important to the answer. You want to remove the warning right? also, i am not the close vote, this is a perfectly valid question (if it isn't a duplicate of something) – Kevin B Jul 02 '14 at 15:06
  • 1
    My suggestion is to first comment out the .val() usage to ensure that it is caused by the :selected psudo selector. If the warning still happens, replace :selected with a .filter. – Kevin B Jul 02 '14 at 15:08
  • Then I do not know who was voting for closing for that dumb reason as I do not find anything unclear on my question. Is there anything, be so kind and point that out. – shadyyx Jul 02 '14 at 15:08
  • @KevinB it is the `:selected` pseudo selector, as you can see here: http://stackoverflow.com/questions/8389841/using-jquery-to-determine-selected-option-causes-specified-attribute-is-depreca – shadyyx Jul 02 '14 at 15:10
  • 1
    then simply replace it with `.filter`. `$('select option').filter(function (i) { return this.selected; })` – Kevin B Jul 02 '14 at 15:13

2 Answers2

2

I prefer do that in a different approach. See the code.

Inside view.

events:
    "change input":"updateModel"
    "change select" : "SelectedItem" //Just for example.

selectedItem:(element)->
    selectedOption = element.target.selectedOptions
    alert **selectedOption.item().value** // the value of the selected item. now you can set it on model.

updateModel:(element)->
    @model.setNew(element.target.name, element.target.value)

Inside model.

setNew:(newName, newValue)->
    @set newName, newValue

Html file.

<select>
<option value="renan">Renan</option>
<option value="carvalho">Carvalho</option>
</select>

<input type="text" name="customer" />

Hope it helps.

rcarvalho
  • 790
  • 1
  • 4
  • 14
  • This works with `` but not at all with `` itself and since instead of element's `name` I need it's text... – shadyyx Jul 02 '14 at 15:19
2

To *fix* it without changing anything else, you can just use .filter.

$('select option').filter(function (i) { return this.selected; }).text();
Kevin B
  • 94,570
  • 16
  • 163
  • 180