Well it if you have something like this in your HTML
<div id="newh2placeholder"></div>
you can use the following if you want to add to the current elements in the div
var newHeader = $("<h2>"+$(this).text()+"</h2>");
$("#newh2placeholder").append(newHeader);
or you can use this to completely replace the content
$("#newh2placeholder").html(newHeader);
But depends on exactly what you want to do. I mean basically, you just have to remember that the string that goes inside of the $(str)
expression is a CSS selector, so if you have multiple matches will cause it to get repeated in multiple places, so if your html is like this
<div class='someClass'></div>
...
<div class='someClass'></div>
And you use the class CSS selector then the action will be repeated in multiple places
//to append
$(".someClass").append(newHeader);
//or to replace inner html
$(".someClass").html(newHeader);
This will cause the action to get repeated in both
I also like to make a function to wrap my HTML Tag elements in
function wrapInTag(tag,target){
return $("<"+tag">"+target+"</"+tag+">");
}
from there you could make a function that takes a tag, target data and a css selector and does the work for you
function appendToSelection(selector,tag,data){
toAppend = wrapInTag(tag,data);
return $(selector).append(toAppend);
}
function setInnerHtmlForSelection(selector,tag,data){
newHtml = wrapInTag(tag,data);
return $(selector).html(newHtml);
}
And then you just use the function in your code
data = $(this).text();
tag = "h2";
selection = "#newh2placeholder";
//if you want to append
result = appendToSelection(selection,tag,data);
//if you want to replace the html
result = setInnerHtmlForSelection(selection,tag,data);