0

here is my html output:

<script language="javascript">
 function append_suggestion() {
 $('input[name="cn"]').append($('#rand-character').val());
 }
</script>
<form name="form" action="/search/results" method="get" class="search-form" id="search-autocomplete">
 <input size="28" class="search-field" id="cn" name="cn" type="text" value="" /> <input type="submit" name="" value="" class="button" />
</form>
<a href="#" id="rand-character" onclick="append_suggestion()">link</a>

After clicking the link nothing happens (it's supposed to insert link text into the input field). How to correct this link behavior?

Thank you.

asked Jan 3, 2010 at 23:05
4
  • What exactly are you trying to accomplish here? Commented Jan 3, 2010 at 23:12
  • 2
    Also: use ´<script type="text/javascript">´ instead of ´<script language="javascript">´ Commented Jan 3, 2010 at 23:13
  • 1
    I don't believe you can append to an input tag: append() inserts DOM elements to the matched element (in this case, all input fields with the name 'cn'). If you're trying to insert the contents of the #rand-character element, try using val(). Also, you should use '#cn' instead of 'input[name="cn"]' if you're only interested in that single input element: he latter must do a more exhaustive search of the DOM, which is considerably slower. Commented Jan 3, 2010 at 23:15
  • @Jan, sorry, edited my question. Commented Jan 3, 2010 at 23:16

1 Answer 1

1

Try this:

var cnEl = $('#cn');
cnEl.val(cnEl.val() + " " + $('#rand-character').text());
Jan Hančič
54k17 gold badges99 silver badges101 bronze badges
answered Jan 3, 2010 at 23:22
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.