javascript - How can I replace an input with a textarea preserving all attributes using jQuery? - Stack Overflow

I have a text input that I'd like to replace with a textarea, preserving all of the attributes. Ho

I have a text input that I'd like to replace with a textarea, preserving all of the attributes. How can I do this with jQuery?

Sample input:

<input type="text" name="newfeature" id="newfeature"
  class="form-required" tabindex="3"
  aria-required="true" />

Desired output:

<textarea type="text" name="newfeature" id="newfeature"
  class="form-required" tabindex="3"
  aria-required="true"></textarea>

I know this can be done manually using the .prop() selector and specifying each attribute/property, but how can I do it dynamically?

Also, this is not necessary, but would it possible to preserve the bindings when doing this?

I have a text input that I'd like to replace with a textarea, preserving all of the attributes. How can I do this with jQuery?

Sample input:

<input type="text" name="newfeature" id="newfeature"
  class="form-required" tabindex="3"
  aria-required="true" />

Desired output:

<textarea type="text" name="newfeature" id="newfeature"
  class="form-required" tabindex="3"
  aria-required="true"></textarea>

I know this can be done manually using the .prop() selector and specifying each attribute/property, but how can I do it dynamically?

Also, this is not necessary, but would it possible to preserve the bindings when doing this?

Share Improve this question asked Mar 3, 2012 at 20:55 cwdcwd 54.9k55 gold badges171 silver badges199 bronze badges 2
  • 1 <textarea> elements don't have a type attribute... – Šime Vidas Commented Mar 3, 2012 at 21:08
  • @ŠimeVidas the .prop() takes care of that. It also sets the value property straight. – David Hellsing Commented Mar 3, 2012 at 21:32
Add a ment  | 

3 Answers 3

Reset to default 5

This also works with the value attribute correctly:

$('input').each(function() {

    var attrs = {};        

    $.each(this.attributes, function() {
       attrs[this.name] = this.value;
    });

    $(this).replaceWith($('<textarea>').prop(attrs));
});​

Example: http://jsfiddle/FyYDT/

ADDED

If you want jQuery events to pass along (not remended), you need to re-bind them. Something like:

$('input').click(function() {

    alert('hey');

}).each(function() {

    var attrs = {},
        ta = $('<textarea>');

    $.each(this.attributes, function() {
       attrs[this.name] = this.value;
    });

    $.each($(this).data('events'), function(i, f) {
        $.each(f, function(){
            ta.bind(i, this.handler);
        });
    });

    $(this).replaceWith(ta.prop(attrs));

});​

http://jsfiddle/FyYDT/1/

I would simply loop through the inputs attributes:

function makeInputTextarea(inputElem) {
    var $textarea = $("<textarea></textarea>");
    $.each(inputElem.attributes, function(i, attrib){
        var name = attrib.name;
        var value = attrib.value;
        if(name === "type") return; // textareas don't have a "type" attribute
        $textarea.attr(name, value);
    });
    return $textarea;
}
$(function() {
    $('input[type="text"]').parent().each(function({
        $(this).html($(this).html().replace(/(<\/)input/gi, "$1textarea").replace(/(<textarea[^>]*)value\=['"](.*)['"]([^>]*)\/>/, "$1>$2</textarea>"));
    }));
});

Should do the trick?

You could do a

$('input, textarea').live('click', function() {
    // do actions!
});

To preserve bindings

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745207580a4616665.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信