Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an item to drop down list with Jquery at a certain index

Is this possible?

I use append like this

$(".ddl").append($("<option></option>").val("").text("Select"));

But this appends it at the end....

like image 866
sarsnake Avatar asked Nov 15 '11 19:11

sarsnake


People also ask

How do I add options to a Dropdownlist using jQuery?

Method 1: Append the option tag to the select box The option to be added is created like a normal HTML string. The select box is selected with the jQuery selector and this option is added with the append() method. The append() method inserts the specified content as the last child of the jQuery collection.

How do I select a specific Dropdownlist using jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How show selected value from database in dropdown using jQuery?

Answer: Use the jQuery :selected Selector You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.

How can set the selected value of dropdown in jQuery using ID?

Using the jQuery change() method; you can set the selected value of dropdown in jquery by using id, name, class, and tag with selected html elements; see the following example for that: Example 1 :- Set selected value of dropdown in jquery by id.


2 Answers

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.

like image 178
Niels Avatar answered Sep 20 '22 12:09

Niels


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")); 
like image 45
gilly3 Avatar answered Sep 21 '22 12:09

gilly3