views:

35

answers:

1

I am using this technique: http://stackoverflow.com/questions/2670327/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 != "")<br>
        $("#country").val(document.getElementById("txtHidData").value);<br>
        //or<br>
        //$("#country")[0].selectedIndex = document.getElementById("txtHidData").value;<br>

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;

....

+2  A: 

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
This answer is more correct withdrawing mine.
Avitus
@Avitus: does not work after i did the post back, please see the above updated source code - thanks
Abu Hamzah
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 `<br>`s?
Matt Ball
Sorry... yea its you "Bears will eat you" :)
Abu Hamzah
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 valuewhat i am doing wrong?please see above - thanks.
Abu Hamzah
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