Replicate val() content in span

Asked

Viewed 108 times

2

I found this code that works very well in 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 wanting to work on other elements like 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 doesn’t work, but if I put any ready text in place of $(email). insertAfter ai works, there is a way to function as it works in input?

2 answers

2

The normal would be to add the span selector and use the html() method to insert the text in 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>
<input type="text" name="email" placeholder='email' />
<input type="text" name="login" placeholder='login' />
<span id="spanLogin"></span><span>

  • worked very well, and the difference between your in . html and the friend below . text, his has more performance, but as our friend is with less medals, I gave the vote to him, thank you very much.

2


To insert the text in by the id="" you use . text(). follows 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>

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

  • yours worked as well as the top, but then I saw that you used the text that works perfectly, the top one is with html, I saw that in performance html is better, but, you have less medals than the top one, so I will choose yours as a response, thank you very much.

Browser other questions tagged

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