Catching select and adding attribute to it
Hello.
Very strange situation: I'm trying to make event for adding "selected=selected" attribute to <option> tag.
So, I have option value Id - <option value='22'></option>
I would like to push selected attribute but doesn't work.
$('select[name="my_id"] option[value=' + element + ']').attr('selected', 'selected').
$('select[name="my_id"] option[value=' + element + ']').prop('selected', true).$(' option[value=' + element + ']').prop('selected', true).
I tried any combinations - with #id instead of select[name=..], also I replaced element with existing id, but nothing fired.
When I make console.log($('select[name="my_id"] option[value=' + element + ']')), I'm seeing prevObject.
What could be problem?
The last possible thing could be that I'm trying to achieve that in a loop.
$(".ids_collect li").each(function () {
var element = $.trim($(this).text()); // element is here
$('select[name="my_id"] option[value=' + element + ']').attr('selected', 'selected'); // nothing
});
I have 2 selects on my page with different ids/names if it's needed as info. Actually, I'm using the same approach to select dynamically the first select. But I'm using there option:contains and looking for text().
Thank you in advance.