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

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.

Related

HTML textbox presentation for large strings

I am working with an HTML input element that is about 10 to 12 characters in size. A user is free to enter any length of string into this field. However, when the user is finished and leaves focus of the textbox, the field shows the latest portion of the string. I want it to show the initial part of the string.
Is this possible?
Sample HTML:
<label><input type="text" name="input" id="text_field" /></label>
One jQuery, dirty&hack-ish, but working solution (not sure about CSS or other possibilities):
$('body').on('blur','#text_field', function() {
new_field = $(this).clone();
$(this).remove();
$('label').append(new_field);
});
DEMO> http://jsfiddle.net/9zswjtqe/4/
Idea is - clone (deep) element on blur, remove it, and attach new element/clone (with desired text position this time). Tested in Firefox, IE, Chrome. Works fine.

Aria-Live="Assertive" is not read by JAWS

Hello Stackoverflow community. This is my first question, but I will try to be as concise and detailed as possible.
I have been tasked with updating our ASP.NET web applications to be section 508 compliant. This is all very new to me, and I'm having trouble getting things to work as expected.
We have a page where the user gets additional information about a link via an onmouseover event. Obviously this doesn't work for non-sighted users. So we are providing them with a "More information" button that displays the same "tooltip" div that the sighted user gets.
I added aria-live="assertive to the "tooltip" div with the understanding that if the div was hidden when the page loads and then shown via the button, it would be read by JAWS. To my dismay, this wasn't the case.
The tooltip div looks like this:
<div id='tooltip' aria-live='assertive' style='display:none;'>
<span id='tooltip_message'>This is my tooltip text</span>
</div>
It is shown via the click event of the button with the following JavaScript code:
function ShowTooltip(ttID)
{
var tt = $('#' + ttID);
tt.css('display', '');
}
The button to show the tooltip div looks like this:
<button id='ttBtn' onclick="ShowToolTip('tooltip'); return false;">
More information
</button>
I had success getting JAWS to read the tooltip by adding role="alert" to the tooltip div, but I'd like to avoid using the alert role for non-alert purposes. Mainly because it reads "Alert, this is my tooltip text." to the user.
I'd like to know what is the proper method to get jaws to read the tooltip when it becomes visible?
I am using IE 11 and JAWS 16. Internet Explorer and JAWS are requirements that I cannot change.
Thanks!
-Ray
UPDATE
I thought I'd post the solution we used in case others have the same problem. This is a simplified version of our code that shows just what's necessary to get the tooltip visible and read when it is displayed. This is a server control, so many of the ID's are based off of the control's ClientID property and have known suffixes (_tootip, _tooltipcontainer, etc.)
JavaScript:
//handles showing/hiding of the tooltip
function ShowToolTip(ttID)
{
var tt = $('#' + ttID + '_ToolTip');
var ttContainer = $('#' + ttID + '_ToolTipContainer');
var ttClone = tt.clone();
tt.remove();
ttClone.css('display', '');
ttContainer.append(ttClone);
}
//Closes the tooltip and returns focus to the hidden (from sighted users) button that shows it.
function CloseToolTip(ttID)
{
var tt = $('#' + ttID + '_ToolTip');
tt.css('display', 'none');
}";
Markup:
<button id="tooltip1_508KeyboardButton" class="hidden" onclick="ShowToolTip('tooltip1'); return false;" onblur="CloseToolTip('tooltip1');">Click for overview</button>
<div id='tooltip1_ToolTipContainer' aria-live='polite' aria-atomic='true'>
<div id='tooltip1_ToolTip' class='section tooltip' style='display:none;'>
<span id='tooltip1_Msg'>This is my tooltip's text.</span>
</div>
</div>
I hope this is of use to someone in the future. As I think about it, I could have easily placed an aria-live region that stays visible somewhere off screen that changes it's text when the tooltip is "shown". So there are many ways to skin this particular cat.
As a quick hack, the following seems to work for me with IE11 + JAWS 15
function ShowTooltip(ttID)
{
setTimeout(function(){
$('#' + ttID).css('display', 'block');
}, 100)
}
You could also try the following:
Push the text of your tooltips to an aria-live region that is always available "on screen" to screen readers
Shift the user's focus dynamically to the tooltip. (I'd be very careful about doing this, though. It can be confusing to your users.)

Is it possible to disable textarea's multiline option?

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);});
});

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

easy way to show image next to textbox?

I trying to validate a form using ajax...I have got it to show either a cross or tick if the box passes valdation. This is done by showing or hiding a div tag, is there an easier way without me having to have a div tag for each cross & tick as this would use about 20 div tags.
Thanks
You could use the CSS :before property to do this:
.tick:before {
content:url(tick.gif);
}
.cross:before {
content:url(cross.gif);
}
(you'll probably have to tweak the CSS a bit to get the image to display in the proper position)
Then in your javascript, just add the class tick or cross to each text box that you want to display an image next to.
Since your validation depends on javascript you could leave the divs out of the html initially and insert them after validating a field. Let's say your source is something like:
<p><input id="email" name="email" type="text" value="" /></p>
Your script could be something like:
var emailField = document.getElementById('email');
if( isValid(emailField) ){
var tick = document.createElement('DIV');
tick.className = 'tick';
emailField.parentNode.appendChild(tick);
}else{
var cross= document.createElement('DIV');
cross.className = 'cross';
emailField.parentNode.appendChild(cross);
}
You add the classes '.tick' and '.cross' to your css and apply background images. Of course, you need some checking if the element was already inserted by a previous validation. This is just a simple example.
This way you don't need the extra divs initially.