Created
August 31, 2012 21:30
-
-
Save bminer/3559343 to your computer and use it in GitHub Desktop.
Revisions
-
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ /* x is the <input/> element type is the type you want to change it to. jQuery is required and assumed to be the "$" variable */ function changeType(x, type) { if(x.prop('type') == type) return x; //That was easy. try { return x.prop('type', type); //Stupid IE security will not allow this } catch(e) { //Try re-creating the element (yep... this sucks) //jQuery has no html() method for the element, so we have to put into a div first var html = $("<div>").append(x.clone()).html(); var regex = /type=(\")?([^\"\s]+)(\")?/; //matches type=text or type="text" //If no match, we add the type attribute to the end; otherwise, we replace var tmp = $(html.match(regex) == null ? html.replace(">", ' type="' + type + '">') : html.replace(regex, 'type="' + type + '"') ); //Copy data from old element tmp.data('type', x.data('type') ); var events = x.data('events'); var cb = function(events) { return function() { //Bind all prior events for(i in events) { var y = events[i]; for(j in y) tmp.bind(i, y[j].handler); } } }(events); x.replaceWith(tmp); setTimeout(cb, 10); //Wait a bit to call function return tmp; } }