I am trying to copy text from list item in unordered list to a text box.
<div class='this_section_is_created_dynamically_and_repeats_up_to_99_times'>
<label>Serial</label>
<input name="part" class= "part" type="text">
<ul>
<li class="pattern"><a href="#" class="clickme">test1</a></li>
<li class="pattern"><a href="#" class="clickme">test2</a></li>
</ul>
<script>
$('.clickme').live('click', function() {
alert($(this).text()); // this works
// try to copy value clicked to input box. these do not work
$(this).prevAll('[input:text]').val($(this).text());
$(this).prev(":text").val($(this).text());
});
prev()andprevAll()look at siblings. The input is a sibling of yourulwhich is the parent of yourlis which are the parents of youranchors:These should work:
Or:
You could also give your
inputan id and just select it directly rather than traversing the DOM.PS. You should close your input tag:
<input name="part" class= "part" type="text" />