1

I am using this technique: is there a way to hold the values? - lost in postback

After it does the postback, how do I set which item is selected?

$(document).ready(function() { 
     if (document.getElementById("txtHidData").value != "")
        $("#country").val(document.getElementById("txtHidData").value);
        //or
        //$("#country")[0].selectedIndex = document.getElementById("txtHidData").value;

Does not work either way, any help? Thanks.

EDIT:

$("#country").change(function() {
         debugger
         var _selected = $("#country option:selected").val();
         document.getElementById("txtHidData").value = "";
         document.getElementById("txtHidData").value = _selected;
         // $("#txtHidData").value = _selected;

....

Community
  • 1
  • 1
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406

1 Answers1

3

I don't know exactly what your markup looks like, but here's a shot:

$(document).ready(function () {
    var val = $("#txtHidData").val();
    if (val !== "") {
        $("#country > option[value=" + val + "]").attr("selected", "selected");
    }
    ...
});

Try not to mix jQuery and the native DOM functions if jQuery provides equivalent functions. Doing so defeats the purpose of using jQuery.

Edit: had some incorrect open/close quotes.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • @Avitus: does not work after i did the post back, please see the above updated source code - thanks – Nick Kahn Apr 20 '10 at 17:27
  • Abu, was that meant for me (not Avitus)? Also, the code you posted needs some cleanup. Would you mind formatting it correctly and removing the extraneous `
    `s?
    – Matt Ball Apr 20 '10 at 17:33
  • Sorry... yea its you "Bears will eat you" :) – Nick Kahn Apr 20 '10 at 17:55
  • okay, i figured out and its working now but one thing left as you have sugguested that i should not mix jquery and dom (thanks for the advice) here is what i am trying to struggle but does not get the value what i am doing wrong? please see above - thanks. – Nick Kahn Apr 20 '10 at 17:57
  • To set the value, use [jQuery.val()](http://api.jquery.com/val/#val2) - is that what you're asking? That is, change your commented line to `$("#txtHidData").val(_selected);` and you should be good to go. – Matt Ball Apr 20 '10 at 18:01