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

Continuing the chunked file upload process using blueimp jQuery-File-Upload will allow for all uploads to be completed simultaneously

I am currently working on integrating file upload functionality using blueImp jQuery-File-Upload with angularJS. The file upload feature must support chunked uploads and should automatically resume the process if a chunk upload fails. Although uploading ...

Reposition the jQuery tooltip arrow

I need help adjusting the position of the arrow to the left near the text box. Can anyone assist with this? Here's what I've tried so far: Check out the working example here: http://jsfiddle.net/b8fcg/ HTML: <input id="test" title="We ask ...

How to Reveal or Conceal a Div Element Automatically After a Short Delay

Looking for a way to hide and show a Div element at specific intervals? I have a solution that operates automatically after certain time frames. However, the code provided is not yielding the desired outcome. setTimeout(function() { $("#myDiv").hide( ...

The cascading style sheet does not have any effect on the layout of the tables within the

I am trying to apply a CSS style to give all tables on my HTML pages a 1px border width. However, for some reason, the borders are not showing up in my HTML files! What could be causing this issue? <!-- <style type="text/css"> --> body{backg ...

The issue of the jQuery dialog failing to close after an AJAX login

Currently, I am implementing a JQuery UI Dialog for a popup login box on my MVC3 application. The functionality is working to log the user in, refresh the "login-status" div on the parent page, and display validation errors in the popup if the login is uns ...

Tips for positioning a chat box at the bottom of a v-card's visible area

My goal is to create a chat app using Vuejs 3 and Vuetify 3, but I'm encountering an issue aligning the chatbox with the v-card component. Instead of being positioned at the bottom of the v-card, the chatbox (green) appears at the bottom of the page. ...

Creating a personalized LinkedIn share button through POST requests: here's how

I am interested in using my own image for the LinkedIn share button. After referencing a previous post on this topic: How to make a custom LinkedIn share button It was mentioned that: In order to create a custom Share button for LinkedIn, POST calls need ...

What is the best way to bring in an external webpage using Jquery and Custom variables into an Iframe?

Is it possible to use JQuery to load a page into an iframe? I have a specific page that generates a custom printable PDF and I want it to be loaded into an iframe for user convenience. Since I utilize jQuery to fetch all the necessary variables, I cannot ...

To make a JavaFX label stand out, simply prepend a vibrant red asterisk at the start of

I'm facing a challenge with my SceneBuilder project. I have developed a scene that consists of several labels, and I want to highlight some of them by adding a red asterisk at the beginning of their text. Unfortunately, I haven't been able to fin ...

Utilizing jQuery for smooth window scroll snapping

I am currently working on creating a page with panels that span 100% of the width and height. <body> <div class="panel"> </div> <div class="panel"> </div> <div class="panel"> </div> </body> The ...

The styling in Docker for Asp.Net Core is not being properly displayed

I recently deployed my first ASP.NET Core application using Visual Studio for Mac inside a Docker container on Linux. However, I'm facing an issue where the CSS is not being applied and the JavaScript is not functioning as expected. It seems like the ...

Is it possible to customize the background color of the 'rows per page' selector box in Bootstrap 4 bootstrap-table?

The text is set in white on a dark grey background by default and appears just below the formatted table. Best regards, Phil Please refer to the attached image:Section of table showing rows per page selector box ...

Customize blueimp-jQuery-File-Upload to upload files to a specific folder

My current project involves using the blueimp fileupload feature to upload images linked with specific property records. Each property's images are stored in a subdirectory of files that is identified by the record ID, such as files/1/img.jpg. In ord ...

CSS dimensional changes

I am currently working on developing an application that involves incorporating a perspective map with the ability to add map markers represented by absolutely positioned DIVs. However, I seem to be encountering challenges related to transformations and 3D ...

Visual Latency when Quickly Toggling Between Images in Succession

I have a series of 50 images that need to be displayed sequentially inside a div. The time interval between displaying each image is initially set at 750 milliseconds and decreases with each subsequent image. To ensure all images are loaded before the an ...

Layering elements using CSS and HTML to create a background effect

I have created a menu using div elements and I want one of the menu items to have a background that extends behind it to the body. Here is the structure: body { background-image: url(path/body.png); } #menu { overflow: hidden; width: 400px; ba ...

The ability to scroll horizontally for individual items within a larger container div

JSFIDDLE: http://jsfiddle.net/7zk1bbs2/ If you take a look at the JSFiddle project, you'll notice that the icons are placed inside an orange box. However, there is a vertical scroll needed to browse through them and I am attempting to enable horizont ...

Display the div only when the radio button has been selected

I have been attempting to tackle this issue for quite some time now, but unfortunately, I haven't had any success. My goal is to display a specific div on the webpage when a particular radio button is selected. While I have managed to achieve this by ...

What is the best method for retrieving JSON POST data in ASP.NET Web Forms?

I have implemented some jquery code on my website to post data. Currently, I am testing the code by posting JSON data. However, I am facing difficulties in retrieving the data on the back-end once it has been posted. Typically, I have used Request.Params ...

Having trouble with Laravel 5.5 and ajax file uploads?

I am encountering an issue with Laravel 5.5 while trying to get an ajax file. I can successfully receive the file using $_FILES, but $request->file() is not working as expected. Here is the code in question. Below are the HTML & Ajax elements: <htm ...