Removing fields from a list

Asked

Viewed 42 times

0

I have a question. I created a list and wanted to remove its elements by clicking on 'x' as in the following image (I put x by Paint):

inserir a descrição da imagem aqui

How can I do it this way? It is an attribute or is configurable by another language??
Pardon the ignorance

<input list="sel-opts" id="new-opt" />
<datalist id="sel-opts">
	<option>#</option>
    <option>@</option>
</datalist>

  • What is the HTML/Javascript of this list?

  • I’ll edit with the code for a moment

  • I put a basic code from a list

1 answer

3


You will use jQuery or javascript for this, and can also be done only with CSS.

I’ll give you an answer with jQuery, just add an event 'click' to remove button, and remove the nearest li element searched with 'closest', in this case I used the method hide(), but there are also other methods.

Following functional example:

$(document).on('click','.remover',function(){
   $(this).closest('li').hide();
});

$(document).on('click','.mostrar',function(){
   $(this).closest('ul').find('li').show();
});
.remover {
text-decoration: none;
float:right;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
  coisa 1 <a href="#" class="remover fa fa-minus"> </a>
  </li>
  <li>
  coisa 2 <a href="#" class="remover fa fa-minus"> </a>
  </li>
  <li>
  coisa 3 <a href="#" class="remover fa fa-minus"> </a>
  </li>
  <li>
  coisa 4 <a href="#" class="remover fa fa-minus"> </a>
  </li>
  <li>
  <a href="#" class="mostrar"> (Mostrar tudo)</a>
  </li>
</ul>

PS: There are several answers that would answer this question, in my opinion jQuery makes life easier.

  • Great idea !! I will try something here based on what I have, just a moment

  • If you can’t put the list html together in the question there

  • I put the of the datalist, at first I need to remove the items from the datalist

  • You are using this plugin http://projects.sergiodinislopes.pt/flexdatalist/ ?

  • No, so far I’m only wanting to delete predetermined items

  • You can inspect the element of 'x' right ? takes some classname from it, puts it in the click event, and fires it into the '$('element x') event. Closest('option'). remove()' almost the same thing as the example, only changes the elements pointed

  • Pults forgot to explain

  • That x I put on the pad, I couldn’t even put it there

  • Ata, so answer me a few things. is this an autocomplete ? Are you using any CSS framework ? Are there any external libraries besides jQuery ? so I knew what would be the best answer.

  • No no, only redirected images without functions, the main code I edited in the question that is the data list, I need to remove the items already added by <option>#</option>

  • Take a look at the answer agr, if that’s what I’ll explain

  • Perfect !! already helps a lot, thanks @Anthraxisbr, I will study the code, anything I ask ok??

  • Nice, just another tip, take a look at CSS frameworks instead of doing things with images that end up weighing a lot, I recommend Bootstrap CSS, makes it much easier to create templates and saves work.

  • Beauty!! Thanks :D

Show 9 more comments

Browser other questions tagged

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