Is it possible to disable textarea's multiline option? - html

Of course, I can use standard html text box, but I need text area for some reason.
So, is it possible to disable textarea's multiline option?

You can set the size of your textarea using the cols and rows attributes and make it non-resizable in CSS but you can't prevent the user from adding more than one line. Even if the area is just too small, a scrollbar will appear and let them write as many lines as they want.
If you really need to get just one line, I suggest setting the rows attribute to 1 and checking if the input has a single line with Javascript.
In html:
<textarea name="a" cols="5" rows="1" ></textarea>
In CSS:
textarea{
resize: none;
#You can use resize: horizontal if you just want to disable vertical resize
}
Still, I'd suggest using an <input type="text" ... /> Why do you want to avoid it?

You can keep all the text on a single line by settings rows="1" on the <textarea>, like the other answers have suggested, and then applying the following CSS to the text area:
resize: none;
white-space: nowrap;
overflow-x: scroll;
This CSS will prevent the single row from wrapping, while still making the keeping the whole line visible by providing a scroll bar to navigate any text that overflows the visible area.

Yes, it is possible using input event to remove all new-line characters every time a user inputs something.
<textarea oninput="this.value = this.value.replace(/\n/g,'')"></textarea>
Or in a more civilized way:
html
<textarea id="ta"></textarea>
js
document.getElementById('ta').addEventListener('input', function(e){
this.value = this.value.replace(/\n/g,'')
});
Demo on JSFiddle

You can use wrap="off" attribute in textarea to be displayed in a single line.
<textarea wrap="off"></textarea>

Use this pure JS code and it will not allow user to enter multiple lines in textarea
var textArea = document.getElementsByTagName("textarea")[0];//your desired TextArea
textArea.onkeydown = function (e) {
if (e.keyCode == 13) { e.preventDefault(); }
};
NOTE: This method will not work if user copies a multi line text and pastes it in the TextArea.

Use rows=1 to display only 1 line. you can get further more information from this link

You can set the rows attribute to 1.
It is not possible to disable the multiline

The following jQuery snippet forces textarea to be displayed in a single line.
$(document).ready(function(){
$('textarea').each(function(){$(this).attr('rows', 1);});
});

Related

Text Overflow Issues with Printing an HTML Form

I'm having some issues with text-overflow when trying to print an HTML form.
You can see the issue in action at:
https://www.yandasmusic.com/Articles.asp?ID=304
In any given form field, if the data entered is longer than the width of the field itself, that extra data is cut off, and not printed. How can I avoid this? Is there a way with CSS to achieve a fluid text box that re-sizes based on the content entered?
Thanks,
Alex
if you want a fluid text box that re-sizes based on the content entered try this http://jsfiddle.net/tdhLyazo/
$(document).ready(function(e) {
$("input:text").keyup(
function()
{
var size = $(this).prop('size')
if($(this).val().length > size)
{
$(this).prop('size',size+1)
}
}
)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="text" size="5">

HTML: How to make a readonly textarea copyable in iOS-devices

How do I make a textarea and input type="text" highlightable and copyable on iOS-devices?
This does not work:
<textarea readonly="readonly">Totally readonly, cannot be copied</textarea>
Neither does:
<textarea disabled="disabled">Totally readonly, cannot be copied</textarea>
EDIT: The text-area is constantly being updated, so a one-off transformation of it won't work.
The content of the textarea can also be HTML.
I have a JSFiddle that I tested this on: http://jsfiddle.net/sebnilsson/jfvWZ/
One solution could be to find all the readonly textareas on the page and render a div with the contents in place of the read only field. I have written some very simple JS to demonstrate this.
Something along the lines of
$('textarea[readonly]').removeAttr('readonly').each(function () {
var $this = $(this);
$this.hide().after('<div data-textarea="' + $this.attr('id')
+ '" class="textarea">' + $this.val() + '</div>');
}).on('textareachange', function () {
var $this = $(this);
$('[data-textarea="' + $this.attr('id') + '"]').html($this.val());
});
You will also need to trigger the event when you update the textarea value.
For example
$('textarea').val('test').trigger('textareachange');
There's a more extensive example here with examples on the styling etc.
http://jsfiddle.net/ssfUx/3/
I've successfull select some text on my iPhone, but needs many try.
<textarea readonly onfocus="this.blur();">Totally readonly, CAN BE copied</textarea>
and the last : http://jsfiddle.net/jfvWZ/6/
<div>
<label>Plain div</label><br />
<div id="plain-div" onFocus="this.blur();">
Plain div
</div>
</div>
Easy to select the text on iPhone
Likewise ran into this issue.
Not sure if the following is a decent, correct or semantic alternative, but it worked for me.
I simply changed the textarea to a div readonly, same styles applied.
The one drawback is that in JavaScript I couldn't target the div with this['myForm']. It doesn't appear to be a child of the form element in DOM.
Instead I had to get the element by id and set it's innerHTML, rather than set the value as with textarea.
It worked on Ipad 1 IOS5 and Iphone 4s IOS7 I am now able to select and copy text to clipboard.

Is it possible to apply styles to the value of a form field?

I have a form input tag with some text assigned to its value attribute:
<input type="text" name="firstName" value="First Name*" />
Since this is a required field, how do I style the asterisk at the end of the "First Name*" string to be the color red? Is this even possible?... How do you style the values of pre-populated form fields?
Thank you.
I think you're making this more complicated then it should. There may be a way using javascript or jquery but I think that's overkill.
I would just put it on the outside of the box and style it within a <span> tag.
I don't think you can style one character to be red but you could style the whole string to be red.
What I would probably do is set the asterisk outside the input box and style it like this
<input ...value="FirstName"><span style="color: #FF0000;">*</span>
No, it's impossible. You'd have to fake it.
However, that's what <label>s are for!!
<label>First Name* <input name=firstName required></label>
Even if it would be possible, it'd not be a good practice. Data should always be separated from the representation. You don't want anything else but the data that you really need to be posted.
It'd be a good idea to go with the jQuery solution that simply assigns a class to the input field on post if there's an error, e.g. makes the text inside the input red, draws the red border around the input box etc.
If you're using asp .net why not just use the required field validator control and let it do the work for you.
Instead of adding * to the field, I would suggest signifying the required field by modifying the element's styling through CSS and jQuery/JavaScript.
Working example: http://jsfiddle.net/wstGQ/
CSS:
input {
color: #ccc;
}
.required {
color: #FF0000;
border: 1px solid #FF0000;
}
jQuery:
$('input').focus(function(){
var obj= $(this);
var oldVal = obj.val();
obj.blur(function(){
if(obj.val() == oldVal){
obj.val('Please enter a valid option').addClass('required');
}else{
obj.removeClass('required');
}
});
});

Put text in textbox

Is there a way to put text in a textbox but also allow the user to type something. I would like to write "username:" inside the box and allow the user to type after the colon. I can do this the hard way by creating a div right next to a textbox and make it look like they are one container, but I was wondering if there was an easier way? Thanks
EDIT: I don't want to text to disappear. I just want to user to be able to continue typing
EDIT 2: the reason you cant put a value in the textbox is because its a form. when the user types a username next to the value it will submit together
HTML5 has a placeholder attribute you can now use:
<input type="text" placeholder="username" />
People have also created javascript functions that mimic this functionality.
There's also a jQuery placeholder plugin which does the same, if you'd like to go that route.
What's wrong with using standard HTML? You don't say that you need it to disappear...
<input type="text" value="username: " />
If you need it to disappear, use a placeholder attribute and a jQuery plugin as a fallback (for the browsers that don't support it.
You could do something like this:
<div>
<label>Username:</label>
<input type="text" />
</div>
CSS
div{border:1px solid gray;}
label{font-family:arial; font-size:.8em;}
input{border:none;}
input:focus{outline:none;}
Basically, created a containing div and placed a label and input in that div. label is the words that stay in the field. input has the border removed.
http://jsfiddle.net/jasongennaro/rZmFx/
Fyi... you may need to increase the size of the input, depending on how many characters you want to accept.
<input type="text" placeholder="Category"/>
Maybe that can help you. If you want the textbox for only read you can put the property readonly = "".
You could call this javascript function once the page is loaded:
function add(text){
var TheTextBox = document.getElementById("Mytextbox");
TheTextBox.value = TheTextBox.value + text;
}
If you are using HTML5, you can use the placeholder attribute.
http://www.w3schools.com/html5/att_input_placeholder.asp

avoid ie contentEditable element to create paragraphs on Enter key

On InternetExplorer, a contentEditable DIV creates a new paragraph (<p></p>) each time you press Enter whereas Firefox creates a <br/> tag.
Is it possible to force IE to insert a <br/> instead of a new paragraph ?
Here's a solution (uses jQuery). After you click on the 'Change to BR' button, the <br> tag will be inserted instead of the <p></p> tag.
Html:
<div id='editable' contentEditable="true">
This is a division that is content editable. You can position the cursor
within the text, move the cursor with the arrow keys, and use the keyboard
to enter or delete text at the cursor position.
</div>
<button type="button" onclick='InsertBR()'>Change to BR</button>
<button type="button" onclick='ViewSource()'>View Div source</button>
Javascript:
function InsertBR()
{
$("#editable").keypress(function(e) {
if (e.which == 13)
{
e.preventDefault();
document.selection.createRange().pasteHTML("<br/>")
}
});
}
function ViewSource()
{
var div = document.getElementById('editable');
alert('div.innerHTML = ' + div.innerHTML);
}
These links helped.
Working example here.
Yes it is possible to avoid the insertion of paragraphs by stopping the keydown event first (window.event.stopPropagation();) and then inserting the string by using insert HTML command.
However, IE depends on this divs for setting styles etc. and you will get into trouble using <br>s.
I suggest you using a project like TinyMCE or other editors and search for an editor which behaves the way you would like, since they have all kinds of workarounds for different browser issues. Perhaps you can find an editor which uses <br>s...
You can always learn to use SHIFT + ENTER for single line returns and ENTER for paragraph returns. IE behaves like MS Word in this respect.
Changing the line-height of the <p> inside the editable <div> works:
#editable_div p
{
line-height: 0px;
}
If you can use it, FCKEditor has a setting for this