Steps to display an angled bracket to indicate a line break

I have a div that is editable, allowing me to display the entered text. To indicate each new line, I would like to show a ">" symbol at the beginning of the line.

HTML

<body>
      <div> 
            <span> &gt; </span> 
            <div contenteditable="true" id="textField"></div> 
      </div>        
</body> 

CSS

body > div > span
{
    float: left;
    padding-top: 10px;
}

body > div > div
{
    outline: 0;
    display: flow-root;
    margin-left: 15px;
    padding: 10px;
}

jQuery

$('#textField').keyup(function(e)
{
   if(e.keyCode == 13)
   {
      var inputGiven = $('#textField').text();
      alert(inputGiven);
   }
});

Currently, when I press enter, my text is alerted and a new line appears with the ">" symbol in its original location:

> Some Text

Some more text

I want the ">" symbol to move to the beginning of each new line like this:

> Some text

> Some more text

And then remove the last ">" symbol like so:

Some text

> Some more text

Answer №1

Utilize the :before pseudo-element for your editable DIV, and employ .before() when pressing Enter to add a non-editable DIV

var $textField = $('#textField');
var $textFieldWrapper = $('#textFieldWrapper');

$textField.on("keydown", function(e) {

  if (e.which === 13) {
    e.preventDefault(); // stop the browser from default Enter behavior
    
    var value = $.trim($textField.text()); // Trimmed value

    $textField.before("<pre>" + value + "</pre>"); // insert noneditable DIV with value
    $textField.html(""); // Reset value of editable element
    $textFieldWrapper.scrollTop( $textFieldWrapper[0].scrollHeight ); // Scroll wrapper
  }

});
#textFieldWrapper {
  border: 1px solid #ddd;
  height: 6em;
  padding: 8px 0;
  overflow-y: scroll;
  background: #363839;
  color: #0bf;
}

/* CLI lines */
#textFieldWrapper > pre {
  position: relative;
  min-height: 1em;    /* set some min-height */
  margin: 0;
  padding-left: 24px; /* 24px left space used for the > pointer */
}

/* CLI editable field */
#textField {
  outline: 0;
}

/* CLI editable pointer */
#textField:before {
  position: absolute;
  left: 8px;
  content: "\3e";
}
<div id="textFieldWrapper">
  <pre contenteditable id="textField">GO</pre>
</div>

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with CSS 3 gradient compatibility in Internet Explorer 8

My CSS3 gradient button is working perfectly on all browsers except for IE8. To make it work on IE8, I am utilizing CSS3 Pie. You can see the search button in action by visiting: Upon inspecting my console, I noticed an error on this line: PIE.htc, lin ...

What is the best method for managing file storage and retrieval in web applications that only run on the client-side?

I'm currently working on an application where users can create their own data. Without a backend or database, all the information is stored within the user's session. My preferred solution involves: User interacts with the app, clicks "Save", ...

I am attempting to create basic animations using css, but I'm having some trouble

Currently, I am attempting to add animation to a css div without including the top property in the original div style. I omitted it because I wanted to delay the animation, so I placed it within the @keyframes property. However, the element disappears afte ...

All you need to know about the properties of CSS ul

When I specify .a ul{} .a ul li{} as well as .b ul{} .b ul li{} will the properties of ul in .b be completely isolated from the properties of ul in .a? Or will they have some commonalities? I am looking to ensure that the styles for ul remain entirely ...

Odd Behavior when altering button attribute using Jquery

After disabling a button with a click handler, I have noticed an interesting behavior where the button does not submit afterwards. The issue seems to vary between different browsers, with Firefox still allowing form submission after the button is disabled, ...

Filtering dynamically generated table rows using Jquery

I'm currently working on a project that involves filtering a dynamic table based on user input in a search bar. The table contains information such as name, surname, phone, and address of users. Using jQuery, I have created a form that dynamically ad ...

Troubleshooting problem with div width in Outlook causing table cell padding issue for curved corners on another div

This question has significantly evolved following additional testing I am using a table layout similar to this: <table> <tr> <td></td> <td style="padding:10px 3%;"> <div border="2px solid #000000;"> ...

Applying the 'overflow: scroll' property creates a scroll bar that remains fixed and cannot be scrolled

Looking to showcase the seating arrangement in a cinema hall. If the seats occupy more than 50% of the page, I want a scroll bar to appear. Attempted using "overflow-scroll" without success. View result image <div class="w-50"> <div ...

The col-md-4 class in Bootstrap does not consistently maintain a width of 33%

Currently, I am utilizing bootstrap cards in my project. The number of cards within a card-deck varies dynamically. However, I have encountered an issue where each card should occupy a width of col-md-4 (33%). Everything works fine when there are more than ...

An HTML attribute that functions like autofocus in the select tag

Is there a way to set the default focus on a select tag in HTML, similar to how autoFocus works for textboxes? I am looking to make a select tag automatically focused when the page loads. Is there a method to achieve this? ...

Is there a way to interact with a Bootstrap 5 dropdown in React without triggering it to close upon clicking?

I'm currently working on creating a slightly complex navigation bar using Bootstrap 5 and ReactJS. The issue I'm encountering involves the dropdown menu within the nav bar. Whenever I click inside the dropdown, even if it's just non-link te ...

Iterate through the list of objects and display duplicates only once

var fixtures = [ { "matchday": 1, "homeTeamName": "Arsenal FC", "awayTeamName": "Leicester City FC" }, { "matchday": 1, "homeTeamName": "AFC Bournemouth", ...

Steps to display a JSX component stored in a variable

Presently, I am implementing an if/else statement within my code to determine the content that will be displayed in the JSX element named 'signupMessage'. Subsequently, I render the contents of this 'signupMessage' element. render() { ...

The controller and node js request associated are invisible to my HTML page

Here is my HTML code. I have just created a unique controller for a specific part of the code. <div class="mdl-grid" ng-controller="ValueController"> <div class="mdl-card mdl-shadow--4dp mdl-cell--12-col"> <div class ...

Show the button's value in the textbox and update the selected button's color using JavaScript

I am having difficulty changing the background color of a selected button after displaying its value in a textbox. The code below successfully displays the button value in the textbox, but I can't figure out how to change the background color of the s ...

What is the best way to eliminate HTML <li> bullets using codebehind?

When working in codebehind, I often create an HTML list using the following method: HtmlGenericControl list = new HtmlGenericControl("ul"); for (int i = 0; i < 10; i++) { HtmlGenericControl listItem = new HtmlGenericControl("li"); Label textLabel ...

Using AJAX, extract URL parameters and retrieve corresponding data from MySQL database

Looking to extract a parameter from a URL link that resembles the following: www.example.com/?v=12345 The goal is to utilize AJAX to query a MySQL database for relevant data. Below is the AJAX code snippet: $.ajax({ type:"POST", url:"ajax2.php", data ...

How to keep jQuery horizontal submenu open when clicking on the menu

Seeking assistance with a menu issue. I have a horizontal menu with subitems that display when clicked, but having trouble with the submenu behavior. When clicking on a submenu item, I want it to remain open and show the related items underneath it. For ex ...

Error Alert: The index "dateform" is not defined

I have set up two calendar date pickers and am trying to obtain the input data or retrieve the "post" date. However, upon running my code, I am encountering the following error message: Notice: Undefined index: dateform I am puzzled by this error because ...

Learn how to dynamically add a class to an element when hovering, and ensure that the class remains even after the mouse has

I'm facing difficulty with this task - when hovering over elements, an active class should be added to them. However, when moving the mouse to another section, the active class should remain on the last element hovered. Additionally, the first block s ...