Something like…
- $('#idexame').change(function () {
- var doc = document.getElementById("idexame"),
- option=doc.options[doc.selectedIndex];
- $("#xnome").html(option.text);
- if ( $('[data-key="'+option.value+'"]' ).length===0)
- $(".selecionado").append('\
- <div class="alert alert-warning alert-dismissible" data-key="'+option.value+'>\
- <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>\
- <h4 class="xnome">'+option.text+'</h4>\
- <textarea name="sexames[]" class="form-control" rows="2" placeholder="Observação ..."></textarea>\
- <input type="hidden" name="exameid[]" value="'+option.value +'">\
- </div>\
- ');
- });
Where I store the value in the data-key attribute of the <div> , then check to see if it is already there.
I also changed xnome from an id to a class, as ids have to be unique. Fix your CSS accordingly.
PS I hope I got all those quotes right!