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.
-
What exactly are you trying to accomplish here?Jan Hančič– Jan Hančič2010年01月03日 23:12:19 +00:00Commented Jan 3, 2010 at 23:12
-
2Also: use ´<script type="text/javascript">´ instead of ´<script language="javascript">´Jan Hančič– Jan Hančič2010年01月03日 23:13:48 +00:00Commented Jan 3, 2010 at 23:13
-
1I 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.rpj– rpj2010年01月03日 23:15:20 +00:00Commented Jan 3, 2010 at 23:15
-
@Jan, sorry, edited my question.Den Thomas– Den Thomas2010年01月03日 23:16:49 +00:00Commented Jan 3, 2010 at 23:16
1 Answer 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
nickf
548k199 gold badges660 silver badges727 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js