Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. var namesList = ['johnathan', 'tim', 'greggory', 'ashton', 'elizabeth'];
  2.  
  3. $(function() {
  4. for (var i = 0; i < 5; i++) {
  5. var name = namesList[i];
  6. $('#names').append('<option>' + name + '</option>');
  7. }
  8. var selected_option = $('#names').find('option:selected').val();
  9. var truncated = truncate(selected_option);
  10. $('option:selected').text(truncated.new);
  11.  
  12. $('#names').change(function(){
  13. var selected_option = $(this).find('option:selected').val();
  14. var truncated = truncate(selected_option);
  15. $('option:selected').text(truncated.new);
  16. });
  17.  
  18. });
  19.  
  20. function truncate(selected_option) {
  21. var nameLength = selected_option.length
  22. if (nameLength > 4) {
  23. selected_option = selected_option.substr(0, 4) + '...';
  24. }
  25. return {new: selected_option}
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement