Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap content in a div using jquery

I am trying to wrap content in a div but the problem is the html page is not editable so I am trying other way, using jQuery to wrap all the content in a div following is the html structure

$(document).ready(function() {
  $("hr").before("<div class=wrapElement>");
	$("#disqus_thread").before("</div>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>
<h4>Title Here</h4>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div id="disqus_thread"></div>

Problem opening div is placed on correct position, but closing div is not on correct position, it should be above div id="disqus_thread" but its not there, how do I get it placed on this position?

jQuery version is 1.12.4

thanks in advance

like image 257
Sanjeev Kumar Avatar asked Nov 16 '16 06:11

Sanjeev Kumar


People also ask

How do I wrap a div content?

If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.

What is wrap method in jQuery?

jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function. Syntax: $(selector). wrap(wrappingElement,function(index))

What is wrap method?

WRAP stands for Widen Your Options, Reality-Test Your Assumptions, Attain Distance Before Deciding, and Prepare to Be Wrong. Widen Your Frame. One of the main pitfalls in decision making is having a narrow frame. That means you don't consider possible alternatives that might be better options.

Which tag wraps all the elements around?

Which tag wrap all the elements around? wrapAll( wrappingElement )Returns: jQuery. Description: Wrap an HTML structure around all elements in the set of matched elements.


2 Answers

Or, you can just use built-in jQuery functions:

Here's a fiddle: https://jsfiddle.net/uhr4kuk6/5/

$(document).ready(function() {
$('hr').remove();
  $('h4').wrap('<div class="wrapElement">').prepend('<hr>');
  $('p').each(function() {
  	var getContentWithTags = $(this).clone();
    $('.wrapElement').append(getContentWithTags);
    $(this).remove();
  })
});
.wrapElement {
  background: red;
  padding-top: .5em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<hr>
<h4>Title Here</h4>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<div id="disqus_thread"></div>
like image 24
junkfoodjunkie Avatar answered Sep 20 '22 19:09

junkfoodjunkie


Use nextUntil() method to get all elements upto the div and use wrapAll() method to wrap them using a div element.

$(document).ready(function() {
  $("hr").nextUntil("#disqus_thread") // get elements from hr upto the previous element of #disqus_thread
       .wrapAll("<div class=wrapElement></div>"); // wrap all elements using div
});

$(document).ready(function() {
  $("hr").nextUntil("#disqus_thread").wrapAll("<div class=wrapElement></div>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>
<h4>Title Here</h4>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div id="disqus_thread"></div>
like image 193
Pranav C Balan Avatar answered Sep 22 '22 19:09

Pranav C Balan