1

Why within JQuery, the combobox add method doesn't seem to be recognized as with traditional html combobox here:

http://viralpatel.net/blogs/demo/dynamic-combobox-listbox-dropdown-in-javascript.html

3
  • 2
    I for one don't understand this question. What combobox add method? Commented Mar 2, 2010 at 15:27
  • 1
    Do you mean if you were to use a jQuery selector to get the combo reference rather than using document.getElementById why doesn't .add work?.. if that is the case it's because the selector returns a reference to a jQuery object instead of the element itself. Commented Mar 2, 2010 at 15:30
  • 2
    A select element is not a combobox. A combobox is a text box combined (hence the name) with a drop down menu. Are you talking about a jQuery plugin that simulates a real combobox? Commented Mar 2, 2010 at 15:31

3 Answers 3

5

To get the actual DOM element in jQuery to call DOM methods on, use .get():

$("#myDropDown").get(0).add(option);

Please note though, there's another way to do this in jQuery:

$('#myDropDown').append($('<option></option>').val(myVal).html(optionText));

There's also the Select Box plugin if you're doing a lot more with selects.

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

1 Comment

The later solution is the one which works on all browsers, plus IE (which doesn't like the 'new Option(value,text)' way)... thanks!
1
function MyFunction(myOption)
{
   $("#cbx").attr("innerHTML",myOption);
}

myOption should look something like this:

var myOption = "<option>" + whatever you want + "</option>";

I would ussually create my own Methods so I can Simply call e.g AddDoc(doc);
Even $("#myDropDown").get(0).add(option); feels too long for me.
Id do something like InsertOption(cbx,Option);
This is just an alternative way ^^.

function InsertOption(cbx,option)
{
   $("#"+cbx).attr("innerHTML",option);
}

function AppendOption(cbx,option)
{
   var opt = $("#"+cbx).attr("innerHTML");
   opt += option;
   $("#"+cbx).attr("innerHTML",opt);
}

Sorry if I made any mistakes ^^... junior prog ere :D

Comments

1

If suppose we have 2 combo boxes

  1. state
  2. cities

When you select the state combo box then according to that value it will add items into cities combo box:

 $(document).ready(function () {

        $("#DropDownList1").change(function () {

            if ($("#DropDownList1 option:selected").text() == "Please Select") {
                $("select[id$=DropDownList2] > option").remove();
            }

            if ($("#DropDownList1 option:selected").text() == "Andhra Pradesh") {
                $("select[id$=DropDownList2] > option").remove();
                $("#DropDownList2").append($("<option>" + ("Hyderabad") + "</option>"));
                $("#DropDownList2").append($("<option>" + ("Vijayawada") + "</option>"));
                $("#DropDownList2").append($("<option>" + ("Karimnagar") + "</option>"));
            }
            if ($("#DropDownList1 option:selected").text() == "Tamilnadu") {
                $("select[id$=DropDownList2] > option").remove();
                $("#DropDownList2").append($("<option>" + ("Madhurai") + "</option>"));
                $("#DropDownList2").append($("<option>" + ("Chennai") + "</option>"));
            }
            if ($("#DropDownList1 option:selected").text() == "Karnataka") {
                $("select[id$=DropDownList2] > option").remove();
                $("#DropDownList2").append($("<option>" + ("Bangalore") + "</option>"));
            }

        });
    }); 

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.