Append on Combobox

by Diego Vieira

HTML

<table>
    <tr>
        <td>
            <label for="descricao">Descrição:</label>
        </td>
        <td>
            <input type="text" id="descricao" class="descricao" />
        </td>
        <td>
            <button type="button" id="Add">Adicionar</button>
        </td>
    </tr>

    <tr>
        <td>
            <label for="periferico">Periférico</label>
        </td>
        <td>
            <select name="periferico" id="periferico">
                <option value="-1" disabled selected>Selecione o Periférico</option>
            </select>
        </td>
    </tr>
</table>

JavaScript

$(function(){
    $('#Add').click(function(e){
        e.preventDefault();
        var str = $('#descricao').val();
        var removeEspaco = str.replace(/^\s+|\s+$/g,"");
        
        if(removeEspaco.length != 0)
            $('#periferico').append('<option value="'+str+'">'+str+'</option>');
        else
            alert("Por favor digite uma descrição");
    });
});