-4

I have a DOM structure like

<div id="container">
  <div id="one">Some fancy text</div>
  <div id="two">Some other text</div>
  <div id="three">and that's enough!</div>
</div>

In my structure, programmatically, during the webpage life, a lot of divs get added, removed and moved inside 'container' but i need a final < br > being at the end of the text the last div ('and that's enough!< br >' on div 'three', in this case) at any moment. It is possible to achieve this result by css, like using 'content' on last child of 'container'?

It would be the best solution for I would not have to insert the br in the last div and control any time what is the last div, remove it and re-add it to it if changes. I'm open to other solutions, anyway.

NB: I can't use jquery.

EDIT: It is not a style problem. Truly, I need the br to be there in the html code to solve another more complex problem, so it is not about style and spaces.

EDIT: I didn't mention the real problem for is way more complex than this... I'm making an editor. In this editor, text is inside the div 'container' which is a content editable div and then divided in several internal divs, changing while the user writes. Due to a problem in Firefox, if I don't have one br at the end of the last div, the caret (cursor) acts really weird, and the only solution seems to be keeping a br in that position at any moment.

EDIT: going deeper in the problem... The problem is known. Firefox solves this putting extra divs in html around the text. I don't want extra divs to be added so when a return is pressed, I force the behaviour of adding a simple br (instead of an extra div container, as normally it would be). Doing this, the first time you press a return at the end of the last div, the caret moves correctly (if you keep writing, it writes in the correct position on a new line), but displays in a strange position, like in the middle between the last and the previous line or at the beginning of the previous line. This happens only at the end of the last div. If you have a br at the end of the last div the caret acts normally.

It is shown here too, but proposed solution are not working for me Firefox sets wrong caret position contentEditable with :before

window.addEventListener('keydown', function (event) {
  
  if(event.keyCode === 13){
    event.preventDefault(); // Ensure it is only this code that runs
    console.log("+++ Pressed return and adding a br!");
    addHtmlElementAtCursor('br');
    
    return false;
  }
});

function keyPress(event) {
  
}

function addHtmlElementAtCursor(element) {
  var sel, range, textNode;
  if (window.getSelection) {
      sel = window.getSelection();
      if (sel.getRangeAt && sel.rangeCount) {
          range = sel.getRangeAt(0);
          range.deleteContents();
          if (element==' ') {
            textNode = document.createTextNode('\u00A0');
          }else{
            textNode = document.createElement(element);
          }
          range.insertNode(textNode);

          // Move caret to the end of the newly inserted text node
          range.setStartAfter(textNode, textNode.length);
          range.setEndAfter(textNode, textNode.length);

          sel.removeAllRanges();
          sel.addRange(range);
      }
  } else if (document.selection && document.selection.createRange) {
      range = document.selection.createRange();
      range.pasteHTML(text);
  }
}
<div contenteditable=true>
<div id="one">some text</div>
<div id="two">some other text</div>
<div id="three">some other other text, try to give a return after the laste e of 'here' -> HERE</div>
</div>
cloned
  • 6,346
  • 4
  • 26
  • 38
Sasha Grievus
  • 2,566
  • 5
  • 31
  • 58
  • 2
    you can give some style to last div and work as `br` – Devsi Odedra Oct 08 '19 at 09:21
  • nope, the truth is I need the br to be there to solve another html problem, it's not a style problem – Sasha Grievus Oct 08 '19 at 09:22
  • then you'll need to put a `
    ` in there - CSS can not insert elements (unless you count ::before ::after etc, but these do not insert an element into the HTML - so, you'll need to insert the element yourself - why not always have it there, and insert/remove elements before it
    – Bravo Oct 08 '19 at 09:23
  • you are trying to solve an [XYProblem](http://xyproblem.info/). Having a `br` inserted is an unusual solution, instead, you need to state why you are trying this approach. – Nidhin Joseph Oct 08 '19 at 09:25
  • 1
    you cant put html element in dom using `css` you can add content but not `html` – Devsi Odedra Oct 08 '19 at 09:25
  • I know, the problem it is keeping at any moment only one br at the end of the last div while the last div changes for other divs are added, or removed, or moved – Sasha Grievus Oct 08 '19 at 09:25
  • Possible duplicate of [Injecting HTML via CSS](https://stackoverflow.com/questions/9740134/injecting-html-via-css) – Devsi Odedra Oct 08 '19 at 09:26
  • if div's are constantly being added/removed/moved ... just have the `
    ` there in the first place, and do your adding/removing/moving above it
    – Bravo Oct 08 '19 at 09:26
  • I didn't mention the real problem for is way more complex than this... I'm making an editor. In this editor, text is inside the div 'container' which is a content editable div and then divided in several internal divs, changing while the user writes. Due to a problem in Firefox, if I don't have one br at the end of the last div, the caret (cursor) acts really weird, and the only solution seems to be keeping a br in that position at any moment. – Sasha Grievus Oct 08 '19 at 09:31
  • keeping it at the end of the divs is simple ... how are your divs being added and moved? (removing would never be an issue) ... if you are using `container.appendChild(div)` to add, just use `container.lastElementChild.before(div)` instead – Bravo Oct 08 '19 at 09:37
  • *Due to a problem in Firefox, if I don't have one br at the end of the last div, the caret (cursor) acts really weird* - is this a known issue? Can you create a minimal example that reproduces the "weirdness"? – Bravo Oct 08 '19 at 09:38
  • added an edit to the question – Sasha Grievus Oct 08 '19 at 10:07

3 Answers3

1

Try using the css selector to add a margin instead of a blank line.

#container > div:last-child {
  margin-bottom:1em;
}
ZektorH
  • 2,680
  • 1
  • 7
  • 20
1

If you for some reason need to use JavaScript to insert the <br> tag, here's a way to do it:

function checkBreaks() {

  const container = document.querySelector('#container');
  const existingBreak = document.querySelector('#container br');
  const newBreak = document.createElement("br");

  if (existingBreak) {

    console.log('There\'s already a break in here, let\'s remove it');
    existingBreak.remove();

    console.log('Also let\'s insert a new break at the end');
    container.appendChild(newBreak);

  } else {

    console.log('No breaks, let\'s insert a new one at the end');
    container.appendChild(newBreak)

  }

}

Edit: I modified the code example as a function that checks for a single <br> inside #container. Whenever your editor changes the structure, you can call checkBreaks();.

Cody MacPixelface
  • 1,348
  • 10
  • 21
  • I can't understand why you would ever add a
    like you want to but if you had to this is how I would do it
    – Jon B Oct 08 '19 at 09:33
  • Thank you, but the real problem is keeping it ever at the end of the last div, while divs changes – Sasha Grievus Oct 08 '19 at 09:34
  • Well then you need to convert this into a function and run it every time you update the container contents. All you need to change is that you do a document.querySelector('br') and remove that element before adding it as the last child again – Jon B Oct 08 '19 at 09:56
  • You're very kind. The problem is not only multiple br but br remaining in an old div if other divs are added in the structure and 'last div' becames another div, leaving a br in the previous 'last div'. – Sasha Grievus Oct 08 '19 at 13:42
1

Instead of a 'physical' <br> try the following:

div:last-child::after {
  content: '';
  display: block;
}

The Line Break element is doing nothing else than forcing a break to the next line and you can do the same thing with any element (even pseudo) that is set to display: block;

I hope this helps.

sebil
  • 236
  • 1
  • 7