<p>Given a select with multiple option's in jQuery. </p>

<pre><code>$select = $("&lt;select&gt;&lt;/select&gt;");
$select.append("&lt;option&gt;Jason&lt;/option&gt;") //Key = 1
       .append("&lt;option&gt;John&lt;/option&gt;") //Key = 32
       .append("&lt;option&gt;Paul&lt;/option&gt;") //Key = 423
</code></pre>

<p>How should the key be stored and retrieved?</p>

<p>The ID may be an OK place but would not be guaranteed unique if I had multiple select's sharing values (and other scenarios).</p>

<p>Thanks</p>

<p>and in the spirit of TMTOWTDI.</p>

<pre><code>$option = $("&lt;option&gt;&lt;/option&gt;");
$select = $("&lt;select&gt;&lt;/select&gt;");
$select.addOption = function(value,text){
    $(this).append($("&lt;option/&gt;").val(value).text(text));
};

$select.append($option.val(1).text("Jason").clone())
       .append("&lt;option value=32&gt;John&lt;/option&gt;")
       .append($("&lt;option/&gt;").val(423).text("Paul"))
       .addOption("321","Lenny");
</code></pre>

[{'answer_id': 27231, 'author': 'Lucas Wilson-Richter', 'author_id': 1157, 'author_profile': 'https://Stackoverflow.com/users/1157', 'pm_score': 3, 'selected': False, 'text': '<p>The HTML <code>&lt;option&gt;</code> tag has an attribute called "value", where you can store your key.</p>\n\n<p>e.g.:</p>\n\n<pre class="lang-html prettyprint-override"><code>&lt;option value=1&gt;Jason&lt;/option&gt;\n</code></pre>\n\n<p>I don\'t know how this will play with jQuery (I don\'t use it), but I hope this is helpful nonetheless.</p>\n'}, {'answer_id': 27259, 'author': 'Juan', 'author_id': 550, 'author_profile': 'https://Stackoverflow.com/users/550', 'pm_score': 5, 'selected': True, 'text': '<p>Like lucas said the value attribute is what you need. Using your code it would look something like this ( I added an id attribute to the select to make it fit ):</p>\n\n<pre><code>$select = $(\'&lt;select id="mySelect"&gt;&lt;/select&gt;\');\n$select.append(\'&lt;option value="1"&gt;Jason&lt;/option&gt;\') //Key = 1\n   .append(\'&lt;option value="32"&gt;John&lt;/option&gt;\') //Key = 32\n   .append(\'&lt;option value="423"&gt;Paul&lt;/option&gt;\') //Key = 423\n</code></pre>\n\n<p>jQuery lets you get the value using the val() method. Using it on the select tag you get the current selected option\'s value.</p>\n\n<pre><code>$( \'#mySelect\' ).val(); //Gets the value for the current selected option\n\n$( \'#mySelect &gt; option\' ).each( function( index, option ) {\n    option.val(); //The value for each individual option\n} );\n</code></pre>\n\n<p>Just in case, the .each method loops throught every element the query matched.</p>\n'}, {'answer_id': 27281, 'author': 'Ricky', 'author_id': 653, 'author_profile': 'https://Stackoverflow.com/users/653', 'pm_score': 2, 'selected': False, 'text': '<p>If you are using HTML5, you can use a <a href="http://ejohn.org/blog/html-5-data-attributes/" rel="nofollow noreferrer">custom data attribute</a>. It would look like this:</p>\n\n<pre><code>$select = $("&lt;select&gt;&lt;/select&gt;");\n$select.append("&lt;option data-key=\\"1\\"&gt;Jason&lt;/option&gt;") //Key = 1\n   .append("&lt;option data-key=\\"32\\"&gt;John&lt;/option&gt;") //Key = 32\n   .append("&lt;option data-key=\\"423\\"&gt;Paul&lt;/option&gt;") //Key = 423\n</code></pre>\n\n<p>Then to get the selected key you could do:</p>\n\n<pre><code>var key = $(\'select option:selected\').attr(\'data-key\');\n</code></pre>\n\n<p>Or if you are using XHTML, then you can create a custom namespace.</p>\n\n<p>Since you say the keys can repeat, using the value attribute is probably not an option since then you wouldn\'t be able to tell which of the different options with the same value was selected on the form post.</p>\n'}]
['https://Stackoverflow.com/questions/27219', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1293/']