Replicate val () content in span

2

I found this code that works great on inputs:

<script>
$(function() {
    $("[name='email']").keyup(function() {
        var email = $(this).val();
        $("[name='login']").val(email); }); });

</script>
<input type="text" name="email" placeholder='email' />
<input type="text" name="login" placeholder='login' />

But I made some modifications that wanted to make it work on other elements such as span.

<script>
$(function() {
    $("[name='email']").keyup(function() {
        var email = $(this).val();
        $(email).insertAfter("span.login"); }); });

</script>
<input type="text" name="email" placeholder='email' />
<span class="login"></span>

But it does not work, but if I put any text ready in place of $ (email) .insertAfter it works, is there a way to work as it works in input?

    
asked by anonymous 11.05.2017 / 18:54

2 answers

2

To insert the text in the by id="" you use .text (). follow the code.

$(function() {
    $("[name='email']").keyup(function() {
        var email = $(this).val();
        $("span.login").text(email);;
    }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script><inputtype="text" name="email" placeholder='email' />
<span class="login"></span>
    
11.05.2017 / 19:08
2

The normal would be to add the span selector and use the html () method to enter the text this way:

$("[name='email']").keyup(function() {
  var email = $(this).val();
  $("[name='login']").val(email);
  $("#spanLogin").html(email);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" name="email" placeholder='email' />
<input type="text" name="login" placeholder='login' />
<span id="spanLogin"></span><span>
    
11.05.2017 / 19:08