Include additional content following the text enclosed by the span tag, but do not place it

Take a look at this screenshot:

https://i.sstatic.net/zojKK.jpg

This code snippet allows me to include emoji spans within a DIV.

$('body').on('click', '.selectable-icons', function(e) {
    if (e.target.tagName.toLowerCase() === 'span') {
        document.querySelector('[contenteditable]').appendChild(e.target.cloneNode(true));
        $('[contenteditable]').append(" ");
        var current_text = $('[contenteditable]').html();
        alert(current_text);
        $('.publisher_div').html(current_text);
    }

The issue I'm facing is that a space is not being added after the span tag, preventing me from writing text immediately following it.

Answer №1

$('[contenteditable]').append(" ");
is a function that adds a space to elements containing the attribute contenteditable (such as a span). To place the space after the element, you can utilize http://api.jquery.com/after/:

$('[contenteditable]').after(" ");

Here is a live example:

var container = $("#container");
console.log("Before:");
container.contents().each(function(index) {
    console.log(index, this.nodeType, this.nodeType === 3 ? '"' + this.nodeValue + '"' : this.tagName);
});
$('[contenteditable]').after(" ");
console.log("After:");
container.contents().each(function(index) {
    console.log(index, this.nodeType, this.nodeType === 3 ? '"' + this.nodeValue + '"' : this.tagName);
});
<div id="container"><span contenteditable>This is the span</span></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Answer №2

give this a shot

  $('.publisher_div').append("hello");

Answer №3

Give this a shot:

$('[contenteditable]').append("&nbsp;");

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

Is there a way to make Chrome ask to save a password within an HTML form?

I'm having trouble getting a basic email/password HTML form to prompt Chrome to save the password. Despite following examples, it's not working for me. Here is my code: <form name="loginForm" action=""> E-Mail:<br><input ...

Using AJAX to submit a form to a CodeIgniter 3 controller

I am working on adding a notification feature and need to run an ajax query through the controller when a button is clicked. Here's the script I'm using: $('#noti_Button').click(function (e) { e.preventDefault(); ...

Expand a list item to full width based on the number of items present

Is there a way to make dynamically added list items in a footer stretch across the width of their container? The footer is created in WordPress and the list items are actually widgets. Here is an example: Fiddle I can't manually set the width for e ...

Is the functionality of the jquery trigger('resize') limited to only one instance?

Here are two code snippets that I have: function adjustFooter() { var wrapper = $('#disqus_wrapper').height(); if ( wrapper > 200 ) { $(window).trigger('resize'); } }; var element = $('#disqus_wrapper&apos ...

Opacity of background color only applies to the color

So, here's the scenario - I currently have a background that looks like this: background:url(../img/arrow_lch.png) right no-repeat #2e443a; The challenge I'm facing is how to decrease the opacity of only the background color (#2e443a). Any sugg ...

The Html.BeginForm() function is directing to an incorrect action within my controller

I'm facing an issue with my HTML form that is built using Html.BeginForm(). Even though I specify the controller name and action name, it always redirects to the wrong action. Here is a snippet from my view: @using (Html. BeginForm("AdminPanelResult" ...

what could be causing the token creation issue in node.js when implementing CSRF protection?

Can you explain why the token is not being created in node js CSRF? I have been using this npm package to generate and verify tokens: https://www.npmjs.com/package/csrf Here is the link to the code sandbox: https://codesandbox.io/s/quizzical-resonance-3y ...

Filtering an unsorted list through the URL parameter of #value when the page is first

I am currently working on building a portfolio website and have encountered some difficulties with filtering the portfolio elements using Javascript / Jquery. Since I am new to this, I am keeping things simple by implementing a basic filter that shows or h ...

Creating a table in HTML using the innerHTML property

document.getElementById("outputDiv").innerHTML = ""; document.getElementById("outputDiv").innerHTML += "<table border=1 width=100%><tr>"; for(j=1;j<=10;j++) { document.getElementById("outputDiv").innerHTML += "<td align=center>"+St ...

Utilize Node.js to retrieve the content from a website's body and display it using jQuery

I am attempting to achieve the following goal: Creating an endpoint in Express.js named /content.json. Upon calling this endpoint, I aim to display the HTML content within a label. The server code I have implemented is as follows: var http = require(&ap ...

Animating the background position in jQuery is not working

var items = $('#menu-top-menu').children(); items.hover(function(){ var itemPosition = $(this).position().left; var parentPosition = $(this).parent().position().left; var backgroundPosition = itemPosition - parentPosition; $(&apos ...

Is there a way to implement tab functionality in jQuery UI through an onclick event?

My goal is to enhance an existing jQuery UI Tab container by adding tab information and then updating the contents through re-initialization. To see a demonstration of what I am trying to achieve, take a look at this JSFiddle: http://jsfiddle.net/dmchale9 ...

Skeleton page CSS sub-navigation menu for Internet Explorer 7

I created a webpage using the Skeleton Framework and added a CSS menu with a submenu. While the menu works well in most browsers, I encountered issues with the submenu when using IE7 and lower versions. Instead of appearing below the parent menu item, the ...

Using NextJS: Customizing page names for cleaner URLs

Currently working on a project in NextJS, I've come across an interesting observation. The file name within my pages folder seems to directly correspond to the path displayed in the URL bar. My question is, can I possibly create a structure like this ...

Aligning a large checkbox to the left in Bootstrap 4 styling

Bootstrap 4. <div class="form-group"> <label asp-for="Enabled" class="control-label"></label> <input asp-for="Enabled" class="form-control" type="checkbox" /> <span asp-validation-for="Enabled" class="text-danger"> ...

How can I locate the source of a slight sideways scrollability issue in HTML and CSS?

While creating my website in HTML/CSS, I noticed that there is a slight sideways scroll to it. Instead of perfectly fitting the screen, there are thin bars of white on each side that can be accessed by scrolling left or right. I am trying to determine the ...

Error encountered on page load due to undefined variable thrown by the Express-validator validation process

In the process of constructing a contact form and incorporating express-validator for validation, I am currently focused on error handling. Below is a snippet of the code from my app.js file that pertains to this matter: // CREATE (POST) ROUTE - add new p ...

The designated redirection path, as indicated in the 'next.config.js' file for a particular project, has now been applied to all projects

Something strange is happening... I set a redirect path for the root index page in one of my projects and it worked perfectly, but now all of my other projects are also being redirected to that same path when I try to visit localhost:3000. It's alway ...

Tips for expanding the range of values using the pattern attribute

I am currently working on an input form that includes a pattern attribute. I have noticed that I can assign a specific value like this: pattern="2222" While this restricts the input field to only accept that value, I am now looking to add multiple values ...

Inserting a new row into a DataTable that has pagination activated, and displaying the recently added row

I am using JQuery DataTables to dynamically insert a row into a paginated table. Here is how I initialize my DataTable: $('#tasksTable').dataTable({ "bProcessing": true, "sAjaxSource": "/admin/gettaskstable", "sAjaxDataProp": "Data" ...