0

I'm getting user input from a form and displaying this information in a span based on it's ID. How can I send this info to a tag as well? I want this to display their email and make an mailto: link with the same input.

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $("#email").keyup(update);
    });
    function update(){
    var email = $("#email").val();
    $('#DisplayEmail').html(email);
    }
    </script>

    <form>
        <input id="email" name="email" size="40" />
    </form>

    <a href="mailto:"><span id="DisplayEmail"></span></a>

3 Answers 3

3

Change this:

function update(){
   var email = $("#email").val();
   $('#DisplayEmail')
    .html(email)
    .parent().attr('href', 'mailto:'+email);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Skip the span (it's unncessary, since the a can contain the address as well), and give the ID to the link instead

<script type="text/javascript">
    $(document).ready(function(){
        $("#email").keyup(function() {
            var email = $("#email").val();
            $('#DisplayEmail').text(email).attr("href", "mailto:" + email);
        });
    });
</script>

<form>
    <input id="email" name="email" size="40" />
</form>

<a id="DisplayEmail" href=""></a>

Comments

0
function update(){
   var email = $("#email").val();
   $('#DisplayEmail')
    .html(email)
    .parent().attr('href', 'mailto:'+email);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.