Add option in a select in original order

Asked

Viewed 20 times

-1

Hello, I have a function that allows you to remove and reset options from a <select>, the "problem" is that when it comes to readjusting the option it goes to the end of select instead of remaining in the original order, I thought of using an array adding the values already in the order I need and somehow verifying the position of the Stack 01 in that array, could someone tell me if that shape I said would be valid or has some other simpler way to do it, and how it could put into practice.

I am currently using this method to add, remembering that this select is created by jquery itself, so some forms do not work:

$('select').append(`<option>Stack 01</option>`)

1 answer

1

Use the function prepend:

The . prepend() method inserts the specified content as the first child of each element in the jQuery collection.

$('select').prepend(`<option>Stack 01</option>`)
  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • @Kayobruno I edited (-:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.