0
   <select   id="Select1">
         <option>1</option>
         <option>2</option>
         <option>3</option>
  </select>

alert($("#Select1").val());

this code Is Correct And Return Current Value.

BUT

   <select runat="server"   id="Select1">
         <option></option>
  </select>

function parseXmlQuestion(xml)
{

   $(xml).find("Question").each(function()
  { 
     var value=$(this).find('Text').text()
     $('#<%=Select1.ClientID %>').
      append($("<option></option>").
      attr("value",value).
      text(value)); 
   });

}

alert( $('#<%=Select1.ClientID %>').val());
alert( $('#<%=Select1.ClientID %> option:selected').val());

or

alert( $('#<%=Select1.ClientID %>').text());
alert( $('#<%=Select1.ClientID %> option:selected').val());

or

alert( $('#<%=Select1.ClientID %>').html());
alert( $('#<%=Select1.ClientID %> option:selected').val());

return null or undefined .

ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
  • Is it actually appending the `option` elements? When you inspect it on the client-side (with something like FireBug) what actually happens? The second example adds more variables than just the `runat="server"`. – David Mar 14 '11 at 12:02
  • what does the `<%=Select1.ClientID %>` suppose to return ? – Val Mar 14 '11 at 12:08
  • 1
    Why have you re-posted this question?? http://stackoverflow.com/questions/5297778/get-current-text-select – Andrew Mar 14 '11 at 12:10
  • first of all, you need to put ';' at the end of var value=$(this).find('Text').text() – neebz Mar 14 '11 at 12:11
  • Put an alert within you each() call and make sure it is hitting that – Andrew Mar 14 '11 at 12:11
  • @nEEbz actually its not really required, its good practice but not that will fix his problem :) – Val Mar 14 '11 at 12:12

2 Answers2

0

u need to remove <option></option> from

   <select runat="server"   id="Select1">
         <option></option> <-- your values get added after this so probably the null is coming from here
  </select>

it should be like

   <select runat="server"   id="Select1"></select>
Rafay
  • 30,950
  • 5
  • 68
  • 101
0

Ok you are a bit weird and you are helping us less each time you answer...

Here is what you should do.

  1. test that this returns the correct id. <%=Select1.ClientID %> which should be Select1
  2. check your xml, that is, correct, (use json preferably it's far easier) including the caps of your xml tags. so do either console.log(value) or alert(value) if you haven't evolved yet :)
  3. parseXmlQuestion() should be called somewhere on the page otherwise you just having a sitting duck not being shot or not telling it to run in other words
Val
  • 17,336
  • 23
  • 95
  • 144