Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a DIV as an input

Tags:

html

input

Sorry if this question is very basic, but I'm trying to get back into programming and I'm stuck with this.

I want to have text input into a <div> in HTML, but I don't want the default chat box that comes with <input>. I just want it to directly print to a location within the <div>.

How can I do this?

like image 602
Arkius Azure Avatar asked Jun 06 '16 21:06

Arkius Azure


People also ask

Can you put a div inside an input?

You can't place any element inside an input. An input does not contain any HTML. Save this answer.

Can you use a div for text?

Using <p> tag is the de-facto choice for writing text. However one can use <div> or a <span> tag for writing text depending on the scenario.

Can div be inside form HTML?

It is completely acceptable to use a DIV inside a <form> tag. If you look at the default CSS 2.1 stylesheet, div and p are both in the display: block category.

Can we use div in JavaScript?

Definition and Usage The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute.


2 Answers

You can use contenteditable attribute if you don't wanna submit its value to the server, just like this :

<div contenteditable="true">I'm Editable. Edit me!</div>

So that user can edit the text but it's not gonna be sent to the server and it's just a client side stuff.

But if you need to submit the value to the server you have to use TextArea or Input tag and remove the borders by CSS styles. Say :

<input id="myText" type="text" style="border:none; background: transparent; outline: 0;"/>
like image 56
akardon Avatar answered Nov 05 '22 14:11

akardon


Use a <div>

Make the div itself a text input (as you mentioned in the comments) by using contenteditable attribute:

<div contenteditable="true" style="outline:0px;">Hodor!</div>

JSFiddle

To get the value you can use jQuery $("div").text() or js textContent

To use the <div> inside a <form> add some js to wire it up. You can see an example here.

Use a <textarea>

You can also use a <textarea> if you hide some properties:

<textarea style="resize:none;overflow:auto;
        border:0px;outline:0px;background-color:transparent;">
    Hodor!
</textarea>

JSFiddle

To get the value you can use jQuery $("textarea").val() or js value

like image 13
Jaqen H'ghar Avatar answered Nov 05 '22 14:11

Jaqen H'ghar