Advertisement
Guest User

Scott Cariss

a guest
Sep 10th, 2010
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Check when ready
  2. $(function() {
  3.    
  4.     // Find dropdowns
  5.     $("select.dropdownreplace").each(function() {replaceDropDown(this);});
  6.     // If document clicked anywhere hide drop downs
  7.     $(document).click(function(event){
  8.         $("div.dropdownreplace ul").hide();
  9.     });
  10.    
  11. });
  12.  
  13. function replaceDropDown(that) {
  14.     // Create HTML for new drop down
  15.     // hidden field
  16.     var hiddeninput = $('<input name="'+$(that).attr("name")+'" type="hidden" value="'+$(that).val()+'" />');
  17.     // div
  18.     var dropdowndiv = $('<div id="dropdownreplacement_'+$(that).attr("name")+'" class="dropdownreplace"><span>'+$(":selected", that).text()+'</span><ul></ul></div>');
  19.    
  20.     // loop through values and make li's
  21.     $("option", that).each(function() {
  22.         $("ul", dropdowndiv).append('<li><span>'+$(this).val()+'</span><a href="#">'+$(this).text()+'</a></li>');
  23.         // set click handler for this drop down
  24.         $(dropdowndiv).click(function() {
  25.             $("ul", this).show();
  26.             return false;
  27.         });
  28.         // set click handler for link items
  29.         $("a", dropdowndiv).click(function() {
  30.             // Get name of hidden input
  31.             var nameofdropdown = $(this).parent().parent().parent().attr('id');
  32.             var nameofinput = nameofdropdown.replace("dropdownreplacement_", "");
  33.             // set hidden input value to whats been clicked
  34.             $("[name='"+nameofinput+"']").val($(this).parent().find("span").text());
  35.             // set div
  36.             $("div#"+nameofdropdown+" > span").text($(this).text());
  37.             $("div#"+nameofdropdown+" ul").hide();
  38.             return false;
  39.         });
  40.     });
  41.    
  42.     // Remove drop down then add in replacement html
  43.     $(that).delay(1000).after(hiddeninput);
  44.     $(that).delay(1100).after(dropdowndiv);
  45.     $(that).delay(1200).remove();  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement