Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .append outside tag

Tags:

jquery

I'm completely new to jQuery. To be honest it is my first few days.

And there is my first question.

$(document).ready(function() {
    $('span.head-span').parent().addClass('head-h').append('<div class="clx" />')
});

As a result I have this

<h1 class="head-h"><span class="head-span">This is Some Heading</span><div class="clx"/></h1>

What do I need to do in jQuery so my .clx will appear after . like this

<h1 class="head-h"><span class="head-span">This is Some Heading</span></h1><div class="clx"/>

Thank you very much in advance.

like image 672
Dom Avatar asked Sep 22 '09 21:09

Dom


People also ask

Is append before or after?

The . append insert the parameter element inside the selector element's tag at the last index position, whereas the . after puts the parameter element after the matched element's tag.

Which jQuery function adds HTML content outside a selection?

append( function ) A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert at the end of each element in the set of matched elements.

How do I append to innerHTML?

To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.


2 Answers

You should be able to do this using after() instead of append()

$('span.head-span').parent().addClass('head-h').after('<div class="clx" />')
like image 176
Simon Fox Avatar answered Sep 28 '22 20:09

Simon Fox


If you want the div after the header, don't append it, use the after method:

$('h1').after('<div class="clx" />');
like image 27
Kobi Avatar answered Sep 28 '22 20:09

Kobi