how to manage the dropdown list index when we remove one item from it

Dani AI

Generated

Good that found the built-in remove-by-index approach; was right to ask whether the list came from the database. A few practical points that keep this stable and bug-free:

  • Prefer tracking and restoring selection by the option value (a stable id from your DB) instead of by index. Indexes shift when you remove an item; values do not.
  • If you remove an option client-side, make sure the UI picks a safe fallback when the selected item was removed (either the previous item, first item, or a placeholder).
  • When rendering server-side (classic ASP), emit each option with a value equal to the DB id and mark the matching value as selected when you rebuild the <select>. HTML-encode values to avoid injection.

Small, practical examples:

var sel = document.getElementById('mySelect');
sel.remove(indexToRemove); // HTMLSelectElement.remove
if (sel.selectedIndex < 0 && sel.options.length)
  sel.selectedIndex = Math.max(0, indexToRemove - 1);
<%
selVal = Request.Form("mySelect")
Response.Write "<select name='mySelect'>"
Do While Not rs.EOF
  id = Server.HTMLEncode(rs("id"))
  txt = Server.HTMLEncode(rs("name"))
  If id = selVal Then
    Response.Write "<option value='" & id & "' selected='selected'>" & txt & "</option>"
  Else
    Response.Write "<option value='" & id & "'>" & txt & "</option>"
  End If
  rs.MoveNext
Loop
Response.Write "</select>"
%>

See the DOM remove method for reference: HTMLSelectElement.remove. Handle the empty-list case, rebind any dependent UI, and prefer value-based selection for robust behavior.

hai,

please explain clearly that u do the reterive from the database or any thing

Now I got the solution
At that time I was unaware of function RemoveAT
Thanks for consideration

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.