Adding an item to drop down list with Jquery at a certain index
if you want to add on like index 2 you can do this:
$(".dll option").eq(2).before($("<option></option>").val("").text("Select"));
this means, select index 2, and put the new option before that one.
Find the option at .eq(n)
to specify the index you want the new option at. Then use .before()
to insert the object. (See also .insertBefore()
, .insertAfter()
, or .after()
)
$(".ddl option").eq(n).before($("<option></option>").val("").text("Select"));