Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
I am taking the text written inside a input field to show in a div, but it is working as a code

A input field where the user will write what he wants to display

<input type="text" class="input-text form-control" placeholder="Input Text">
<div id="paste"></div> //code to be pasted here

keyup function called every time user make changes

$("#input-text").keyup(function(){
    $("#paste").append(`<div class="form-control">$(".input-text").value</div>`)})

When I write "<tagname Hello /tagname>" in input field, it does not get displayed, rather it gets implemented as an HTML code. How can I correct that?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think you have a typo here <div id="paste"> //missing one double quote

about 4 years ago · Juan Pablo Isaza Report

0

There is multiple problems with your code, it should be :

$("#paste").append('<div class="form-control">' + $(this).val() + '</div>')
  • It should be $("#input-text").val() not $("#input-text").value Also you can change $("#input-text").val() to $(this).val()
  • You need to wrap your html <div class="form-control">$("#input-text").value</div>}) also you have a } that should not be there.

Demo

$("#input-text").keyup(function() {
  $("#paste").append('<div class="form-control">' + $("#input-text").val() + '</div>')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="input-text" class="form-control" placeholder="Input Text">
<div id="paste"></div> //code to be pasted here

about 4 years ago · Juan Pablo Isaza Report

0

All the typo-issues aside, your issue is that you are using .append with a complete html string.

This will append html.

If you want what you input (eg html tags) to appear as text, then you need to use .text(newtext). As you're wrapping the input in a div, the easiest way is to generate the div, then add the text. You could also encode the text when you add it as html, but using .text(txt) does this for you.

var div = $('<div class="form-control">');
div.text($(this).val())
$("#paste").append(div);

You can make this a single line by using .appendTo(), which returns the new element rather than .append() which returns the outer element, then chain .text().

$("#input-text").keyup(function() {
  $('<div class="form-control">').appendTo("#paste").text( $(this).val() );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="input-text" class="form-control" placeholder="Input Text">
<div id="paste"></div> //code to be pasted here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!