Please tell me how to change the type of the element c 'input' to 'textarea' with a specific class, but that other 'textarea' or 'input' would not be changed using jQuery.

It was:

<input class="field" id="ex" name="ex" type="text" value=""> 

It became:

 <textarea class="field" id="ex" name="ex" type="text" value=""> 

    1 answer 1

     function changeTagName(el, newTagName) { var n = document.createElement(newTagName); var attr = el.attributes; for (var i = 0, len = attr.length; i < len; ++i) { n.setAttribute(attr[i].name, attr[i].value); } n.innerHTML = el.innerHTML; el.parentNode.replaceChild(n, el); } changeTagName(document.getElementById('ex'), 'textarea'); 
     <input class="field" id="ex" name="ex" type="text" value="" /> 

    UPD.

     $("#ex").each(function (o, elt) { var newElt = $("<textarea />"); Array.prototype.slice.call(elt.attributes).forEach(function(a) { newElt.attr(a.name, a.value); }); $(elt).wrapInner(newElt).children(0).unwrap(); }); 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="field" id="ex" name="ex" type="text" value=""> 

    • Thanks, but can I do the same with Jquery? - arantar 2:55 pm
    • @arantar, you can, updated. - meine
    • It will not work, my id is unknown in advance, and here I cited as an example. It is necessary for the class of the element. The essence of the problem: choose a certain select - the input field changes to textarea and vice versa, if you choose another value. Id, class, etc. are saved. Moreover, such blocks with select and input / textarea are created by the user himself, i.e. there is a context. - arantar pm
    • You need something of this, but it does not work correctly: jsfiddle.net/gvof27su - arantar
    • @arantar, next time, please specify the question so that there are no such incidents. - meine