Crayl
Crayl

Reputation: 1911

JQuery - How to put new element around a new generated element

My script generates a new input field and I want to put a 'span' element around it. But I can't figure out, how to do that.

 .append($('<a>').html('+') 
            .attr('href', '#')
            .click(function(){
                ($(this)
                    .after($('<a>').text('X')
                        .attr('href', '#')
                        .click(function(){
                            $(this).parent().remove();
                            return false;   })
                    ) 
                    .after($('<input>') // NEED A SPAN AROUND THIS ELEMENT
                        .attr('type', 'text')
                        .attr("name","avz_anzahl["+avz_array+"][]")
                        .attr("size","3")
                        .attr("style","margin-left:10px; margin-top:5px;")
                        .attr("id","anzahl_feld")
                        .attr("onblur","anzahl_vali();")
                        .attr("value", "")
                    ) 

                    .after('<br/>')).append ;
                return false;   })
    )

Upvotes: 1

Views: 90

Answers (4)

scripto
scripto

Reputation: 2297

You can use the wrap function - http://api.jquery.com/wrap/

Upvotes: 1

dku.rajkumar
dku.rajkumar

Reputation: 18568

try changing the part like

.after($('<span>').html($('<input>')
                        .attr('type', 'text')
                        .attr("name","avz_anzahl["+avz_array+"][]")
                        .attr("size","3")
                        .attr("style","margin-left:10px; margin-top:5px;")
                        .attr("id","anzahl_feld")
                        .attr("onblur","anzahl_vali();")
                        .attr("value", "")
                    ) 
    )

Upvotes: 1

agarcian
agarcian

Reputation: 3965

You can try JQuery wrap.

http://api.jquery.com/wrap/

<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>

    $('.inner').wrap('<div class="new" />');

Upvotes: 4

Kevin Ji
Kevin Ji

Reputation: 10489

You could wrap your element with the <span> element with jQuery's .wrap method:

[yourcodehere].wrap('<span>');

Upvotes: 1

Related Questions