0

I have a Select element which options are being populated dynamically from a database. However, when I attempt to collect these in an array using the standard javascript or jquery methods I come up with a blank... it's as if there are no "options" in my element - except when I do a right-click / "View Source" and inspect it there are options there.

Anyone have a clue about what's going on and what I can do to capture those 'hidden' options?

Attempted solution:

var ProdOptions = new Array;
$("#dataset.recordids.Products option").each  ( function() {
  ProdOptions.push ( $(this).val() );
});

For clarity: The element id="dataset.recordids.Products". The code is being called out after the page is loaded and document ready (in fact I've even put it into a button to test the possibility that it was happening before the data was populating - no luck).

Mitch
  • 377
  • 2
  • 12
  • 1
    What's the `id=` of the select? – freedomn-m Jul 26 '16 at 09:44
  • Exactly when do you call this code? I'm guessing after the content has actually loaded, but worth confirming... – freedomn-m Jul 26 '16 at 09:45
  • Are you sure you want `.val()` and not `.text()`? Can you show (some?) of the output select+options? – freedomn-m Jul 26 '16 at 09:48
  • What's the result of `alert($("#dataset.recordids.Products").length)` and `alert($("#dataset.recordids.Products option").length)` ? – freedomn-m Jul 26 '16 at 09:48
  • Is there another element with 'id=dataset.recordids.Products' ? – freedomn-m Jul 26 '16 at 10:04
  • @freedomn-m: the results of both alerts are 0. There is only 1 id of that name. – Mitch Jul 26 '16 at 10:30
  • @freedomn-m: here is the "View Source" after population: – Mitch Jul 26 '16 at 10:34
  • if both alerts are **0** then your selector is not working. `alert($("#dataset.recordids.Products").length)` should be 1. – freedomn-m Jul 26 '16 at 10:43
  • @freedomn-m - that's not an accurate statement... I am using the select element just fine - in fact there are other related functions which cause data from the same database to populate other fields on the form based on the selection made from this element. I'm certain the issue is that the options are coming from a database - I recall clearly reading something that because it's populated dynamically from a database that they're not 'there' in the same manner as usual... I just can't remember how to fix it. – Mitch Jul 26 '16 at 10:47
  • No, honestly, you can't do `$("#a.b.c")` to match `
    `
    – freedomn-m Jul 26 '16 at 10:49
  • http://stackoverflow.com/questions/605630/how-to-select-html-nodes-by-id-with-jquery-when-the-id-contains-a-dot – freedomn-m Jul 26 '16 at 10:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118301/discussion-between-mitch-lizar-and-freedomn-m). – Mitch Jul 26 '16 at 10:50

1 Answers1

1

You can't use "." in an ID selector as "." means class, as in:

<div class='bold border'>

is selected with

$("div.bold.border")

so

$("#dataset.recordids.Products option")

would be looking for:

<select id="dataset" class="recordids Products"><option>...

You can get around this by double-escaping the "."

$("#dataset\\.recordids\\.Products option")

Tiny fiddle: https://jsfiddle.net/om739rmu/

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • I don't think this is an accurate statement... I'm using them and they're working just fine. – Mitch Jul 26 '16 at 10:49