In fact, what you want is to "add a suffix per line", regardless of any data being inserted.
To insert a suffix / prefix, just take the current value, then "add" the string to be inserted. In the case of textarea
you must break per line. For simplicity, since I'm familiar, I'll use JQuery.
To break by line use .split('\n')
and make loop
:
$.each($('textarea').val().split('\n'), function(index, data){
});
Then also make an array with the results, in addition you said you need to remove other providers (ie remove everything after @
) use .split('@')[0]
and then add the desired suffix:
value.push(data.split('@')[0] + sufixo);
Now you have a value
array with all suffixed data, just merge the whole array and put it in a textarea
, use .join("\n")
to do this.
$('textarea').val(value.join("\n"));
Result
$('#enviar').on('click', function() {
// Array dos resultados:
var value = [];
// String do sufixo (a ser inserido):
var sufixo = $('#inicial').val();
// Executa função em cada linha:
$.each($('textarea').val().split('\n'), function(index, data){
// Se tiver algo escrito remove o sufixo (se já existir!) e adiciona o sufixo:
if(data.trim() !== ""){
value.push(data.trim().split('@')[0] + sufixo);
}
});
// Junta a array inserindo quebra de linhas entre eles
$('textarea').val(value.join("\n"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" class="form-control" name="inicial" id="inicial" value="@hotmail.com" placeholder="@hotmail.com">
<br />
<br />
<tr align="center" bgcolor="#000000">
<td height="10" colspan="2"><span class="texto">Lista de emails</span>
</tr>
<tr align="right">
<td height="136" colspan="2" valign="top">
<br>
<textarea name="emails" style="width:100%" rows="8" wrap="VIRTUAL" class="form" id="emails">
[email protected]
[email protected]
[email protected]
[email protected]
meuemail
</textarea>
<span class="alerta">Click > </span>
</td>
</tr>
<tr>
<td height="26" align="right" valign="top" colspan="2">
<input type="submit" name="Submit" id="enviar" value="Substituir">
</td>
</tr>