8

I have the following html:

<div>
    <input type="text" style="xxxx" size="40"/>
    <input type="text" style="xxxx" size="100"/>
    <input type="text" style="xxxx" size="100"/>
    <input type="text" style="xxxx" size="40"/>
</div>

Now I want to change each input whose size is '100' to a textarea which has the same style as the input.

I have tried this:

$("input[size=100]").each(function(){
  //how to replace it?
});

Any ideas?


I used this solution found in the answers here:

$("input[size=100]").each(function() {
    var style = $(this).attr('style'), textbox = $(document.createElement('textarea')).attr({
        id : $(this).id,
        name : $(this).name,
        value : $(this).val(),
        style : $(this).attr("style"),
        "class" : $(this).attr("class"),
        rows : 6
    }).width($(this).width());
    $(this).replaceWith(textbox);
});
15
  • 1
    This is trivial to Google. Commented Jun 13, 2013 at 9:09
  • 3
    I tried, it does not work. BTW, I do not think my post deserved a down-vote. Commented Jun 13, 2013 at 9:10
  • 3
    Google does not work? It does for me. The first hit I get is e.g. jQuery change hidden input type to textarea - if you have specific problems with that, you should add them to the question Commented Jun 13, 2013 at 9:10
  • google.com.hk/… . This is what I got. Commented Jun 13, 2013 at 9:11
  • 1
    @Pekka Now, this is the first google result, funny how that works. Commented Feb 22, 2022 at 22:48

4 Answers 4

13

jQuery has a .replaceWith() method you can use to replace one element with another. So, copy your styles from the input and use this method, as follows:

$('input[size="100"]').each(function () {
    var style = $(this).attr('style'),
        textbox = $(document.createElement('textarea')).attr('style', style);
    $(this).replaceWith(textbox);
});

Demo

Sign up to request clarification or add additional context in comments.

Comments

3

Try something along the lines of

$('input[size="100"]').each(function()
{
    var textarea = $(document.createElement('textarea'));
    textarea.text($(this).val());

    $(this).after(textarea).remove();
});

Here's an example: http://jsfiddle.net/SSwhK/

Comments

1

Try like this

$("input").each(function(){
      if($(this).attr('size') == "100") {
             my_style = $(this).attr('style');
             $(this).replaceWith("<textarea style='"+my_style+"'></textarea>");
      }
});

4 Comments

You misunderstand me, I just do not know how to replace it.
See my edit....my ans also not worth of downvote as like your question
I agree this didn't deserve a downvote... but why answer such a lazy question in the first place, enabling the user to ask more and more of them? Just sayin'.
Yes @Pekka웃 ,but I thought that it is simple that makes him clarify
0
$(document).ready(function(){
    $("input[size=100]").each(function(){
      //how to replace it?
        $(this).after('<textarea style="xxxx"></textarea>').remove();
    });
});

Check http://jsfiddle.net/6JH6B/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.