As per Javascript Variable within an html value?

Asked

Viewed 229 times

1

I have the following code:

console.log(ArmazenaIds);
console.log(ArmazenaEmails);
contaQuantidadeDeEmails = ArmazenaEmails.length;
//listando emails selecionados na div .top 
for( c=0; c<contaQuantidadeDeEmails; c++) {

  $('ul').append('<li>'+ArmazenaEmails[c]+'</li>');
  // COLOCANDO CADA EMAIL DENTRO DO VALUE PRA ENVIAR PELO FORM
  //USANDO AJAX
  $('#novo').append("<input type='hidden' name='emailzinhos[]' value='aff' /> ");
}

How do I place the vector ArmazenaEmails[c] within the value='aff', I don’t know how to do this, I tried with + and it’s giving me syntax error, it’s a question until easy just don’t know the answer.

2 answers

3

You can concatenate the vector values ArmazenaEmails[c] in string:

for( c=0; c<contaQuantidadeDeEmails; c++) {
  $('ul').append('<li>'+ArmazenaEmails[c]+'</li>');
  // COLOCANDO CADA EMAIL DENTRO DO VALUE PRA ENVIAR PELO FORM
  //USANDO AJAX
  $('#novo').append("<input type='hidden' name='emailzinhos[]' value='"+ArmazenaEmails[c]+"' /> ");
}

In this example, I will remove the hidden of inputs to be able to view the value:

ArmazenaEmails = ['[email protected]','[email protected]','[email protected]'];

contaQuantidadeDeEmails = ArmazenaEmails.length;
//listando emails selecionados na div .top 
for( c=0; c<contaQuantidadeDeEmails; c++) {
  $('ul').append('<li>'+ArmazenaEmails[c]+'</li>');
  // COLOCANDO CADA EMAIL DENTRO DO VALUE PRA ENVIAR PELO FORM
  //USANDO AJAX
  $('#novo').append("<input type='' name='emailzinhos[]' value='"+ArmazenaEmails[c]+"' /> ");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="novo">
   <ul>
   </ul>
</div>

0


With the selection of tag input with the value "aff".

Just add the following statement:

$('input[value="aff"]').val(ArmazenaEmails[c]);

Thus remaining:

console.log(ArmazenaIds);
console.log(ArmazenaEmails);
contaQuantidadeDeEmails = ArmazenaEmails.length;
//listando emails selecionados na div .top 
for( c=0; c<contaQuantidadeDeEmails; c++) {

  $('ul').append('<li>'+ArmazenaEmails[c]+'</li>');
  // COLOCANDO CADA EMAIL DENTRO DO VALUE PRA ENVIAR PELO FORM
  //USANDO AJAX
  $('#novo').append("<input type='hidden' name='emailzinhos[]' value='aff' /> ");
  $('input[value="aff"]').val(ArmazenaEmails[c]);
}

Browser other questions tagged

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