Reveclair
Reveclair

Reputation: 2421

How to add text to some options of a select box?

In a form, I have a select box whose some options are pre selected by ajax according to a previous value entered.
I would like to add additional text to the options (only those which have been pre selected by ajax according to the previous value).

for (i in data) {
$("#my_select_box_id").val(data[i][0])..attr('selected',true); //that works

//I would like to use something like that but it's not working
$("#my_select_box_id").text(data[i][1]);
}

I think I have to get the options pre selected,but that's not working too :

$("#my_select_box_id option[value=data[i][0]).text(data[i][1]);

Upvotes: 0

Views: 73

Answers (1)

Shawn Wernig
Shawn Wernig

Reputation: 1742

$("#my_select_box_id").html(data[i][1]);

To make it selected, you can use:

$("#my_select_box_id").attr("selected", "selected");

*Edit, I'm actually not sure if it will select the option item when you use the above call, W3C specifies that it will on page-load... Can you report back?

--

To select the option elements you want, and to change the label, try:

$("#my_select_box_id option[value=" + data[i][0]+"]").html(data[i][1]);

Upvotes: 3

Related Questions