28

How can I get HTML to work in the value of the input field ? If you include HTML in the value, it appears as pure text. Is there a way to do something like this?

<input type='text' value='<b>tekst</b>'></input>

So the the output is:

tekst

instead of

<b>tekst</b>

I think that was bad example... I want every appropriate HTML tag to work. If i want to include an image, the image appears in the input, if i want to add a tag ... the text should appear as a link linked.

4
  • why not set a style like this to the input input{ font-weight: bold; } Commented Apr 28, 2011 at 19:23
  • no no no , i gave it like an example, i ment every kind of html, Commented Apr 28, 2011 at 19:25
  • 1
    are you trying to create a WYSIWYG editor? like ckeditor.com Commented Apr 28, 2011 at 19:29
  • no, but i need to see if there is some way to do it Commented Apr 28, 2011 at 19:31

5 Answers 5

20

I'm not sure from your question whether you are trying to make the value contain HTML, or whether you want to apply HTML to something inside the input.

If you want to be able to set the value to some HTML, like <input value="<b>Some HTML</b>"> where the value will actually show the HTML, you need to encode the HTML brackets like <input value="&lt;b&gt;Some text&lt;b/&gt;">. There are functions available to do this for you automatically in most scripting languages (e.g. htmlentities() in PHP).

If you want to apply HTML to the input value, short answer is... you can't. If you want formatting on there, you could use the contentEditable attribute in HTML5 which lets you just edit-in-place (think Flickr headers).

If you just want a standard input field to be styled, you can just use CSS like the other answers suggested.

Sign up to request clarification or add additional context in comments.

2 Comments

u saved my life to a totally unrelated problem, was searching over the net how to embed html inside a string which i both wanted to be partly an html element and partly text :)
Htmlentities does the job
2

You can try to decode you entities from your postvalue. In PHP you can do this with html_entity_decode();

Comments

2

You have to encode all the HTML you want in the value and paste it in the input value.

If you encode this:

"http://sansoftmax.blogspot.com/"

It will look like this:

&quot;http://sansoftmax.blogspot.com/&quot;

In input:

value="&quot;http://sansoftmax.blogspot.com/&quot;"

Online Html Encoder/Decoder

1 Comment

This encoder became very helpful. If someone needs to paste HTML code as field value to HTML file in visual studio, first you must encode this string, and then as you paste it in the file, visual studio will ask you, do you want to escape this text. When you refuse to escape, everything works just fine
1

I don't think you can put HTML inside a text field and have it interpreted as HTML instead of as text in the field.

To accomplish what you want you'll have to use CSS. An in-line example to bold the text as you cited in your example:

<input type="text" style="font-weight: bold;" value="tekst" />

5 Comments

@Danish: I've been wondering why this answer has recently gotten a couple downvotes after sitting forgotten for 6 years. From the question it isn't clear (at least to me - and I just realized I tried to clarify it) what the OP wanted. If you have real feedback, please provide it. Unfortunately, "really!! what??!" isn't helpful.
you are clearly angry i will delete my annoying comment sorry!
@Danish: No, I'm not angry. I am really trying to understand why my answer is suddenly disliked.
@GreebMatt so from the question so far what i understood is, OP was to have html saved as value in input tag. His example mights leads you to think think that he wants font weight bolf, but what he meant was to have html saved in input like this '<b>tekst</b>' also he mentioned this "... I want every appropriate HTML tag to work"
Thanks for the feedback. I saw that in the question. I don't know of any way to make that work, and said so in the answer. The top voted question says something similar. If you know a way to make it work, please provide it as an answer.
-2

Try CSS:

input[type=text] {
    font-weight: bold;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.