0

I am using the jQuery load() function to load an HTML file containing a multiple select menu

<select multiple>
<optgroup label="optgroup1">   
<option>A</option>
<option>B</option>
<option>C</option>
</optgroup>
</select>

(there are further optgroups and options in the real code)

In the callback I would like to add a value to each <option> which equals the value of the text, so the final HTML would be:

<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>

The closest I have managed is

$("option").each(function() {
    $("option").attr('value', $(this).text());
});

Unfortunately, when I inspect the code I see that this is adding the text from the last option to each value:

<option value="A">C</option>
<option value="B">C</option>
<option value="C">C</option>

How should I be doing this?

1 Answer 1

1

try this

$("option").each(function() {
    $(this).attr('value', $(this).text());
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.