0

I have a form with a select box whose options are pre selected by ajax according to a previous value entered by the user.

I need to add text to these options pre selected ONLY.

for (i in data) {
        $("#my_select_box_id").val(data[i][0]).attr('selected',true);
        var id_option = data[i][0];

        //this following line doesn't work, why do I do wrong here ?
        $("#my_select_box_id option[value="id_option"]).html(data[i][1]);
}
1
  • You have a missing " - see the colour coding of the problem line. Commented Oct 17, 2012 at 15:52

1 Answer 1

2

Use the :selected selector if you want currently selected options.

$("#my_select_box_id option:selected")

Your code didn't work because you were prematurely closing the selector string instead of concatenating. This results in an error.

$("#my_select_box_id option[value='" + id_option + "']");

Also, the attribute selector should only work on attributes. I don't know about jQuery though because they've historically diminished the distinction between properties an attributes. So it depends if .attr() is actually setting the attribute of the property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.