Transforming text into visual content using a live preview div powered by jQuery

Looking for a way to display images in real-time as a user types letters into a textarea field. When a user inputs a letter like 'k', an image associated with that letter should appear. Currently, only pre-entered letters on the page show images, not text entered into the textarea field. You can check out the [demo] (http://jsfiddle.net/jLzsrygv/8/) for reference.

The method I am using to convert characters to images is outlined below:

$('#target').each(function() {
var txt = $(this).html();
var img = '<img src="http://i.imgur.com/DWwRx9M.png" alt="' + txt + '" />'
var html = txt.replace(/\*/g, img);
$(this).html(html);
});

What I've attempted so far

I've tried adjusting the allowed tags of the textarea field to allow img tags and controlling it with another master textarea, but this approach did not work as expected. The 'k' input does not automatically convert to the img tag in the original textarea field.

Since directly inserting img tags seems to be the only solution, my limited understanding leads me to believe that dynamically replacing typed letters in real-time might provide a viable option. I have experimented with text replacements, but I suspect that my use of textContent instead of value or innerHTML might be causing issues. Check out how far I've come with [this example] (http://jsfiddle.net/hnou29me/).

Any assistance would be greatly appreciated.

Answer №1

It's important to note that the .each() methods need to be called within a keyup event in order for them to run multiple times. Currently, they are only being executed once when the JavaScript is loaded. To address this issue, place the .each() calls inside separate functions and invoke these functions upon the keyup event.

$(document).ready(function(){
    change1();
    change2();
    $('#someTextBox').keyup(function(){
        $('#target').html($(this).val());
        change1();
        $('#target2').html($(this).val());
        change2();
    });
});

function change1() {
    $('#target').each(function() {
        var txt = $(this).html();
        var img = '<img src="http://i.imgur.com/DWwRx9M.png" alt="' + txt + '" />'
        var html = txt.replace(/\*/g, img);
        $(this).html(html);
    });
}

function change2() {
    $('#target2').each(function() {
        var txt = $(this).html();
        var img = '<img src="http://i.imgur.com/DWwRx9M.png" alt="' + txt + '" />'
        var html = txt.replace(/\*/g, img);
        //console.log(html);
        $(this).html(html);
    });
}

Fiddle: http://jsfiddle.net/jLzsrygv/21/

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

Mastering the Bootstrap 'Thumbnail Grid System'

I'm having trouble aligning all the images in my design. Even though I'm using Bootstrap's column classes (class="col-sm-4 col-md-3"), they are not displaying equally. Here is a screen recording showcasing the issue: Click here to see the s ...

What are the steps to customize the color of an icon and text when clicked or when hovering over them

I just entered the world of coding and am currently immersed in a project. Right now, I have a side navbar and I'm attempting to change the color of the icon and text when I hover over it or click on a route. For instance, when I click on the Home lin ...

css hover function not operational with back to top button

I have implemented a back to top button using the following resource: Although the button is functioning as expected, I am encountering an issue with the hover effect. In Chrome inspector, manually forcing hover on .cd-top does not apply the CSS style as ...

The callback function fails to remove the JQuery row from the table

I am facing an issue with deleting a dynamically created row from a grid in my project. I use jQuery to send an Ajax request to the server to delete the corresponding data from the database. If the deletion is successful, I want to remove the row from th ...

Bokeh is having trouble loading from the CDN

I am currently attempting to embed a plot along with its data by utilizing autoload_static within a straightforward html page that I wish to view locally on my computer. According to the documentation, all I need to do is place the .js file in the specifie ...

The reason for the failure of the web API is due to the incorporation of the original controller's name by

I'm attempting to fetch data from a web-api controller within a razor page loaded by a standard controller. However, my $.getJSON() call fails because the getJSON method is appending the original controller name in front of the URL. How can I work ar ...

Propagating numerical values through iterative iterations

I am currently facing an issue with passing values as props to a component using the forEach method in JavaScript. In addition to passing the existing values from an array, I also want to send another value that needs to be incremented by 1 for each iterat ...

Validation of forms and submission of forms through AJAX with the use of jQuery and CodeIgniter framework

My current code for adding comments is as follows: <div id="2" class="789678"> <div id="dynamic2"> <span class="error" style="display:none">Please Enter Valid Data</span> <span class="success" style="display: ...

Error in the Syntax of Project Image Slider

I'm encountering an issue with a WordPress script called Project Slides. Initially, this script was working fine but suddenly stopped. After investigating in the console, I found the following error: VM138 plupload-image.js?ver=4.2.2:67 Uncaught Err ...

Issue with jQuery: Android browser receiving XML instead of JSON from HTTP POST request

When attempting to make an HTTP POST request using jQuery in older Android browsers, I encounter a specific issue. The response received is: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <application xmlns="http://wadl.dev.java.net/20 ...

Make sure that the parent element is only visible once all of its child elements have

As a newcomer to the world of react, I am facing some challenges. I have created a form in react which includes a dropdown. To ensure reusability across multiple pages, I decided to turn this dropdown into a component that is responsible for fetching all n ...

In my attempt to assess the correlation between value 1 and a value in the preceding object, I am utilizing the *ngFor directive

Attempting to compare 2 entries in an *ngFor loop. The code should compare the value at the current object to a value at the previous object. <ng-container *ngFor="let item of s_1.comments[0]; index as b"> <article class="message i ...

When a user clicks on JavaScript, it will return true if clicked and false if

Currently working with selenium Java code and incorporating JavaScript execution using the executeScript method, as seen below: WebElement menu = driver.findElement(By.cssSelector(string1)); ((JavascriptExecutor) driver).executeScript("arguments ...

Using React Bootstrap: Passing an array to options in Form.Control

I'm currently utilizing Form.Control to generate a dropdown list and I want the list to be dynamic. Here is my code snippet: render() { return ( <Form.Control as="select" value={this.state.inputval} onChange={this.updateinputval}> ...

Submit numerous queries to verify the presence of a name and assign it to the name attribute

I have a collection of unique devices. As part of a process, I need to assign a default name to each device ("DeviceX" - where X is a sequential number), but some of the names may already be in use. To handle this, I must make a request to check ...

Setting up web SMS configuration for email integration

I have a code for sending web mails and SMS notifications that is functioning correctly. However, I want to receive SMS notifications only when users select the “A” value from the servicerequired drop-down menu and do not wish to receive SMS between 8 ...

Adjusting the box sizing of a contentEditable DIV to conceal the first and last characters

I am having trouble with a ContentEditable div that requires the hiding of characters, specifically the first and last character. I need to overlay a layer on top of the "#" character to make it appear as if the character and the border line beneath it are ...

Choose all the inputs with the value attribute specified

How can I select all input textboxes in my form that have the required class and no value using jQuery? Currently, I am using: $('input[value=""]', '.required') The issue I am facing is that even when a user enters a value in the text ...

Navbar transition issue in Bootstrap

Having trouble with Bootstrap transitions not functioning correctly on my navbar. The issue is when I click the dropdown button in the navbar, the hidden elements appear abruptly without any transition effect. Below is the HTML code being used: <!do ...

What is the best way to install Bootstrap through npm?

My npm project is utilizing jquery with the following code: var $ = require('jquery'); In addition, my index html file includes a reference to bootstrap: <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="styl ...