0

i have the follow code

$("#tlbQueryConf tr:last").append(
    $('<td></td>').append(
        $(".ddlFields").clone().attr('id', 'ddlFields-' + conf.id).attr('class', '').val(conf.field)));

So now, what i'm trying to accomplish it's give the attribute selected to the new element with that value that i selected

i tried like this, but sets the atribute selected to the element and not the item

$(".ddlFields").clone().attr('id', 'ddlFields-' + conf.id).attr('class', '').val(conf.field).attr('selected',true)

the html that result its the follow

<select name="ctl00$ContentPlaceHolder1$ddlFields" id="ddlFields-1" class="">
    <option value="1">Prueba</option>
    <option value="2">cantidad de fallas</option>
    <option value="3">nº</option>
    <option value="4">Ciudad</option>
    <option value="5">COID</option>
    <option value="6">COID_DESC</option>
    <option value="7">Creado_EN</option>
    <option value="8">DSLAM PROVEEDOR</option>
</select>

Note

even using val() the property selected it's not set to item

thecodeparadox
  • 81,835
  • 21
  • 131
  • 160
Jorge
  • 16,426
  • 17
  • 75
  • 120

3 Answers3

2

If I understand the question correctly (and correct me if I misunderstood), you want to change the selection of the drop down list?

You can do that like this:

$(".ddlFields").val("value of the item you want selected");

and you can retrieve the selected value like this:

$(".ddlFields").val();

You don't have to worry about setting the selected attribute your self, jquery handles that for you when you set the value of the list.

See this duplicate question for more information: Change the selected value of a drop-down list with jQuery

Community
  • 1
  • 1
Kyle Trauberman
  • 24,648
  • 13
  • 83
  • 116
  • i thinked exactly the same but when inspect the html, the option doesn't have the attribute `selected` – Jorge Jul 15 '11 at 16:12
  • 1
    When that script is executed, does the value of the list change? What browser are you using? Some browsers don't reflect changes caused by scripts in their "view source" features. – Kyle Trauberman Jul 15 '11 at 16:14
  • in the view of the browser the item it's change with my value but in the html, the item doesn't have the property set. I added the html. I'm using chrome – Jorge Jul 15 '11 at 16:18
  • 1
    Why are you concerned that the `selected` property is being set, if the value is changing as expected? – Kyle Trauberman Jul 15 '11 at 16:19
  • because, in other part of my script i need that value and when i getting the value doesn't give the same item display in the browser – Jorge Jul 15 '11 at 16:21
  • 1
    To get the selected value of the dropdown you can call `.val() `without any arguments: var value = $(".ddlFields").val(); – Kyle Trauberman Jul 15 '11 at 16:23
  • I know but it doesn't give me the same value that I'm setting – Jorge Jul 15 '11 at 16:28
2

You're adding a selected=selected attribute to the select element when you should be selecting one of the options. Try this:

$("#tlbQueryConf tr:last").append(
    $('<td>', {
        html: $(".ddlFields").clone()
            .attr({'id':'ddlFields-' + conf.id,'class':''}).val(conf.field)
    })
);

http://jsfiddle.net/hunter/aEVt2/

hunter
  • 58,834
  • 17
  • 108
  • 112
1

Which item are you trying to select? As per your code it will add selected attribute to the select tag. You should specify the option to select. Try this

$(".ddlFields").clone().attr({ 'id': 'ddlFields-' + conf.id, 'class': ''}).val(conf.field);
ShankarSangoli
  • 67,648
  • 11
  • 84
  • 121