0

Why doesn't this work?

     <script>
     $(document).ready(function(){
     $('#custom_field option').click(function(){
        $('#custom_field_input').append('<tr><td></td></tr>');      
        $('#custom_field_input td:last').append($(this).find(":selected").text());
     });
     });
     </script>

I found out that there is a .change function for this and it works but does not relate to me as i need to have the text appended even if there is no change in the value of the select dropdown.

Meaning.

User clicks on option1, appends option1 text.

User clicks on option 1 again, another option 1 text gets appended.

Lawrence
  • 717
  • 1
  • 12
  • 25

7 Answers7

0

Make the custon_field_input element on click of custom_field_opotion as empty with following code :

<script>
 $(document).ready(function(){
 $('#custom_field option').click(function(){
  $('#custom_field_input').html('');      
    $('#custom_field_input').append('<tr><td></td></tr>');      
    $('#custom_field_input td:last').append($(this).find(":selected").text());
 });
 });
 </script>
neeraj
  • 740
  • 1
  • 6
  • 21
0
<script>
 $(document).ready(function(){
 $('#custom_field').change(function(){
    $('#custom_field_input').append('<tr><td></td></tr>');      
    $('#custom_field_input td:last').append($(this).val());
 });
 });
 </script>

this should work in your case as well

Surace
  • 701
  • 3
  • 8
0

Try

 <script>
     $(document).ready(function(){
     $('#custom_field option').click(function(){
     $('#custom_field_input').html('');      
     $('#custom_field_input').append('<tr><td></td></tr>');      
     $('#custom_field_input td:last').append($(this).find(":selected").text());
 });
 });
</script>
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

you can also get help from

http://www.guy.footring.net/randomStuff/selectBox.htm

0

You can use blur or focusout events instead of click.

$('#custom_field option').focusout(function(){
....
 });   

$('#custom_field option').blur(function(){
....
 });
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
0

Try This.

jQuery(function () {
    jQuery('#custom_field').click(function () {
        jQuery("#custom_field_input").val(jQuery("#custom_field_input").val() + jQuery("option:selected", this).text());
    });
});
Krishna Kumar
  • 2,101
  • 1
  • 23
  • 34
0

Try this: Made 2 changes. Used live incase your selects are getting populated later dynamically. If not use simple .click(). Also instead of .find(":selected"), use .find("option:selected").

<script>
 $(document).ready(function(){
 $('#custom_field option').live('click',function(){
    $('#custom_field_input').append('<tr><td></td></tr>');      
    $('#custom_field_input td:last').append($(this).find("option:selected").text());
 });
 });
 </script>
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29