1

Please help i am doing this to use in my mobile with jquery mobile

var obj = jQuery.parseJSON(finalresult);
 //alert(obj);
var dept = '2';
$(document).ready(function () {
    //$("#opsContactId").empty();
    $.each(obj, function (k, v) {
        var $opt = $("<option/>");
        $opt.attr("value", k);
        //alert(k);
        //alert(v);
        //var value1=v;
        if (k == dept) {
            //$opt.attr("selected","selected");
            //alert("in if");
            $("#opsContactId").val(k);
            //document.getElementById("opsContactId").value=k;
            // $('#opsContactId').selectmenu('refresh', true);  
        }
        $opt.text(v);
        $opt.appendTo($("#opsContactId"));
    });
});

not able to set an option to be selected by dafault

Satpal
  • 132,252
  • 13
  • 159
  • 168
Aj.
  • 27
  • 1
  • 7

3 Answers3

2

As others as stated, jQuery's .prop() would be the most suitable answer here as jQuery Docs mention themselves:

"The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value."

To further your on your example, jQuery allows for method 'chaining' which returns the jQuery object after the method has completed, thus you can add another method directly after it.

<select id="opsContactId">   
</select>

<script>
$(document).ready(function() {
    var tmp = [ 1, 2, 3, 4, 5 ], 
        dept = 2;

    $.each( tmp, function( k, v ) {
        $("<option/>").attr("value", k)
                      .text( "Value - " + v)
                      .appendTo( $("#opsContactId") )
                      .prop( 'selected', ( k == dept ? 'selected' : '' ));        
    });
});
</script>

Fiddle: http://jsfiddle.net/twdgC/


Later I forgot you mentioned the jQuery mobile aspect of your question, which changes the dynamic of the question a little bit. The snippet above is run after the page has loaded (Thus, all the jQuery mobile attachments have already been set/made), which would happen to give you the result of (below)

Empty Select Option

Fiddle: http://jsfiddle.net/5ksG8/

This obviously isn't helpful when trying to construct an <select> list, thus we'll need to append the snippet above with:

$("#opsContactId").selectmenu('refresh', true );

After the snippet has run, thus it 'reloads' the entire list, finally providing you with (below)

After query

Fiddle: http://jsfiddle.net/YxVg6/

You were doing this in your original snippet, the issue was - you were executing it within the loop (And it was commented out!).

MackieeE
  • 11,751
  • 4
  • 39
  • 56
  • thank you @MackieeE ..its been solved..ya i traced that was the cause for getting empty select.. – Aj. Jan 15 '14 at 13:11
0

jsBin demo

if you have a match you'll need to add the attribute selected. however I don't know how your object looks like...

var obj  = {"1":"one","2":"two","3":"three","4":"four"}; // let's say that's how it looks
var dept = '2';

$(function () {

  var $sel = $("#opsContactId"); 
  $.each(obj, function (k, v) {
    var $opt = $("<option/>", {value:k, text:v}).appendTo( $sel );
    if (k == dept) $opt.prop({selected:true}); 
  });

});
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51
  • @RokoC.Buljan : i am unable to get the output i tried all the ways given below and above...unable to select the values selected..please help – Aj. Jan 15 '14 at 10:50
  • @user3185790 ok, let me take a closer look at what you're trying to do – Roko C. Buljan Jan 15 '14 at 10:52
  • Folks, This is my entire code Roles : – Aj. Jan 15 '14 at 10:59
  • Yes, Roko..even thourgh i cleared the cookies of my browser and cross checked it twice..but still i am not getting it..can i have your gmail id..so that i can take help from you to get into my system – Aj. Jan 15 '14 at 11:08
  • 1
    Mates, Thank you all for responces especially @Roko I got the issue solved here is the code $.each(obj, function (k, v) { $("").attr("value", k) .text(v) .appendTo( $("#opsContactId") ) .prop( 'selected', ( k == dept ? 'selected' : '' )); }); $('#opsContactId').selectmenu('refresh', true); }); i placed the selectmenu so that it got the value selected... Issue is solved now...Thank a lot – Aj. Jan 15 '14 at 11:54
0

Would something like this not be easier?

Would something like this not be easier?

var selected = "selected";   // work this out
var text = "Option Text"; // work this out
var value = "Option Value"; // work this out
$("#opsContactId").append('<option value="' + value + '" '+ selected + '>' + text + '</option>');
slinky2000
  • 2,663
  • 1
  • 15
  • 12