locked
What am I doing wrong here? Using alert, I get a value, but trying to set the following to a variable I get an error RRS feed

  • Question

  • User-858993849 posted

    Using the following, the alert works, but setting e to it as a variable give me an error that "option" is undefined.

    alert($('select[id$="ddlFileNames"] option:selected').text());
    var e = fileName = $('select[id$="ddlFileNames"] option:selected').text();

    What am I doing wrong?

    Friday, October 2, 2015 6:21 PM

Answers

  • User281315223 posted

    What does your markup for this look like? It might make it a bit easier to determine the best selector to use in your scenario.

    Have you tried setting it to a single variable like the following :

    var fileName = $('select[id$="ddlFileNames"] option:selected').text();

    The code that you are using also assumes that there is a selected value, so you would need to make sure that one exists (as otherwise, I suppose you could get a null reference type error like the one you are receiving).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 2, 2015 9:22 PM

All replies

  • User281315223 posted

    What does your markup for this look like? It might make it a bit easier to determine the best selector to use in your scenario.

    Have you tried setting it to a single variable like the following :

    var fileName = $('select[id$="ddlFileNames"] option:selected').text();

    The code that you are using also assumes that there is a selected value, so you would need to make sure that one exists (as otherwise, I suppose you could get a null reference type error like the one you are receiving).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 2, 2015 9:22 PM
  • User-858993849 posted

    Hi Rion, I know I can always count on you!  The error was actually coming from somewhere else where I had the same code.  Thanks again for your help.

    Saturday, October 3, 2015 9:20 AM