Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a delete/remove button per each option item in html select?

I need to create or use a library to create a custom HTML <select> to delete each item in the HTML <select>. Something like this:

<select>
    <option value="volvo">Volvo</option> <button>delete</button>
    <option value="saab">Saab</option> <button>delete</button>
    <option value="mercedes">Mercedes</option> <button>delete</button>
    <option value="audi">Audi</option> <button>delete</button>
</select>

I have searched how to do this and for any library that might exist to do this, but I haven't found anything. In iOS exists this. I need something like that but for HTML.

UPDATE: Something like this http://jsfiddle.net/b22ww/2/

like image 806
epool Avatar asked Sep 18 '13 19:09

epool


People also ask

How to remove select option in HTML?

Select remove() Method The remove() method is used to remove an option from a drop-down list.

How to add delete button in table in HTML?

Add a Delete Button to Each RowModify the productAddToTable() function (Listing 4) to include a button control as you add each row of data. In the JavaScript you write to build the <tr> and the <td> elements, add one more <td> that includes the definition for a <button> control.

How do you add a delete in HTML?

The <del> tag defines text that has been deleted from a document. Browsers will usually strike a line through deleted text.

How to remove option value in select by JavaScript?

The option to be removed is selected by getting the select box. The value to be removed is specified on the value selector (value='optionValue') on the select box. The remove() method is then used to remove this selected option.


3 Answers

Use this:

<ul>
    <li><input type="radio" name="list" value="volvo">Volvo <button>delete</button></li>
    <li><input type="radio" name="list" value="saab">Saab <button>delete</button></li>
    <li><input type="radio" name="list" value="mercedes">Mercedes <button>delete</button></li>
    <li><input type="radio" name="list" value="mercedes">Mercedes <button>delete</button></li>
</ul>

Then add event listeners to each button that removes the parent <li> from the list (Demo at jsfiddle.net).

like image 108
Bergi Avatar answered Oct 05 '22 09:10

Bergi


What you need to achieve, it's quite impossible using select. What you need is to create something like a listview, like this:

HTML

<ul>
    <li>item one <div class='deleteMe'>X</div></li>
    <li>item two <div class='deleteMe'>X</div></li>
    <li>item three <div class='deleteMe'>X</div></li>
    ....
</ul>

and bind a click handler

JS

$(".deleteMe").on("click", function(){
   $(this).closest("li").remove(); 
});

see this example

FIDDLE http://jsfiddle.net/zZ3mc/

like image 42
BeNdErR Avatar answered Oct 05 '22 08:10

BeNdErR


If you are open to using jQuery plugins, chosen allows you to customize select elements. In your case, look at the section called "Selected and Disabled Support".

like image 20
Christophe Avatar answered Oct 05 '22 10:10

Christophe