jQuery Form How to - Set clicked element as Value for text input








Question

We would like to know how to set clicked element as Value for text input.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from w w w .  j  a va 2 s. co  m-->
    $('a').click(function(){
        $('#text_tag_input').val($(this).html());
    });
});
</script>
</head>
<body>
  <input id="text_tag_input" type="text" name="tags" />
  <div class="tags_select">
    <a href="#">tag1</a> <a href="#">tag2</a> <a href="#">tag3</a>
  </div>
</body>
</html>

The code above is rendered as follows: