2

So I have been stumped with this for a while now and can't really seem to get around it. I have some content that is added through a wysiwyg editor. What I want to do is use the <hr> element to add divs in there to control the look of the container. The problem is that it adds the closing div. I want to add the closing div to the next hr element.

var firstChild = jQuery('.content-container-wrapper hr:first-child');
var lastChild = jQuery('.content-container-wrapper hr:last-child');
if (firstChild) {
    jQuery(firstChild).replaceWith('<div class="working">');
};

Is there a way to tell the browser not to add the closing div?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Barrett
  • 25
  • 4

1 Answers1

0

It sounds like you want to wrap everything starting with the first HR and ending with last.

Following will wrap everything between the two HR and then remove them( my interpretation of your question).

var $start=jQuery('.content-container-wrapper hr:first-child'),
    $end=jQuery('.content-container-wrapper hr:last-child');

$start.nextUntil( $end).wrapAll( '<div class="working">');
$start.add($end).remove();

You can't insert part of an element and then insert the end the way you do in a text editor

API reference:

http://api.jquery.com/wrapAll/

http://api.jquery.com/nextUntil/

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • It is saying that Uncaught TypeError: Object [object Object] has no method 'replace' – Barrett Mar 03 '13 at 23:52
  • provide some sample html in jsfiddle.net, will get it working there. Also use a better browser console to check errors, Chrome or Firebug in Firefox...that looks like an IE error and others are far more informative – charlietfl Mar 03 '13 at 23:52
  • Looks like it is working in jsfiddle. I think I have an issue with the jQuery version. http://jsfiddle.net/EvR9f/ – Barrett Mar 04 '13 at 00:02
  • save fiddle and post link... I wrote this untested – charlietfl Mar 04 '13 at 00:03
  • not sure what you want...using `last-child` is wrapping everything...and going past the second HR. Can use `first` and `last` instead. Some color helps to see what is wrapped http://jsfiddle.net/EvR9f/1/ – charlietfl Mar 04 '13 at 00:19
  • It's kind of working how I need it. Would it be possible to make it for each set of
    tags? http://jsfiddle.net/xuXB5/
    – Barrett Mar 04 '13 at 01:18
  • not clear...wrap items between each HR and remove all the HR? – charlietfl Mar 04 '13 at 01:20
  • HA! That is it. You are the man! I was working with it but couldn't quite get it there. Thanks! – Barrett Mar 04 '13 at 01:30