admin管理员组文章数量:1315310
If I've got a textarea like this:
<textarea id="foo" cols=80 wrap="hard"></textarea>
Is there any way, using JavaScript, to grab the hard-wrapped text?
I've tried the obvious $("#foo").val()
, but that returns the unwrapped text.
For example, if the textarea is:
<textarea id="bar" cols=5 wrap="hard">a b c d e f</textarea>
The browser will wrap the text to:
a b c
d e f
And I was some way to grab that text – "a b c\nd e f"
(which, because of wrap=hard
, is the text that would be sent if the <textarea…
was submitted in a <form…
).
If I've got a textarea like this:
<textarea id="foo" cols=80 wrap="hard"></textarea>
Is there any way, using JavaScript, to grab the hard-wrapped text?
I've tried the obvious $("#foo").val()
, but that returns the unwrapped text.
For example, if the textarea is:
<textarea id="bar" cols=5 wrap="hard">a b c d e f</textarea>
The browser will wrap the text to:
a b c
d e f
And I was some way to grab that text – "a b c\nd e f"
(which, because of wrap=hard
, is the text that would be sent if the <textarea…
was submitted in a <form…
).
- Did any of these answers help you? – Brian Webster Commented Jul 23, 2012 at 16:16
6 Answers
Reset to default 2Since the formatting is done by the browser on submit, your best bet will be to have the browser submit, but to a location of your choice such as an iframe where you can extract the formatted text from the form or url parameters.
<html><body>
<script type="text/javascript">
function getURLParameter(qs, name)
{
var pattern = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( pattern );
var res = regex.exec( qs );
if (res == null)
return "";
else
return res[1];
}
function getHardWrappedText(){
if (top.location.href !== window.location.href) return;
var frm_url = document.getElementById('ifrm').contentDocument.URL;
if (frm_url.indexOf('http') < 0) return;
var text = unescape(getURLParameter(document.getElementById('ifrm').contentDocument.URL, 'bar')).replace(/\+/g,' ');
alert(text)
}
</script>
<form name="main" method="get" target="ifrm">
<textarea id="bar" name="bar" cols=5 wrap="hard">a b c d e f</textarea>
<input type="submit">
</form>
<iframe id="ifrm" name="ifrm" onload="getHardWrappedText();" style="display:none;"></iframe>
</body></html>
I'm assuming that you know the dimensions of the text area (if not, you can probably just get them from the DOM).
Use this to split the string. Go to the xth char (in your example the 5th), and see if the next char is a space. If so (or if there are no spaces in the preceding segment), this is where you split. Otherwise, you find the preceding space and split there.
Wrap this in a function and you could add in some recursion to find the nth line.
Edit 2
The only solution that i can propose is to create a hidden iframe with a name, and target the form submit to that frame. Then the iframe can municate with its parent throught the window.parent
and provide the returned text (which will include the new lines as the form has been normally submitted..)
of course the submit could be called by a script if you wished ..
the following i have strike-through since it does not apply to this case..
a generic way to do this would be
alert( $('<div/>').append($('#foo').clone()).html() );
what this does is
Create a new div (in memory)
$('<div/>')
clone the desired element #foo and append it to the div we created
.append( $('#foo').clone() )
extract the inner html (but since we are dealing with the div that we wrapped around our element, we get the elements html ..
.html()
[EDIT]
obviously i misunderstood .. to get the inside text of an element use the .text() method
so
$('#foo').text();
With modern browsers the wrapped text can be found in FormData:
new FormData(document.querySelector('form')).get('name')
This appears to match what is submitted (the wrapping as shown or the unwrapped value).
I don't think it's possible out of the box You could wrap the text yourself using textarea's cols
value. From the HTML point of view the text in textarea is not wrapped. It's just how it's being displayed for you to be able to read it better.
PS. If you are using jQuery you should use $('#foo')
instead of $('[id=foo]')
selector as it's much faster (and shorter).
It's a bit of a hack, but you could create a script on the server that prints out whatever is posted to it in some format (JSON, plain text, whatever suits you) and post your textarea to that script via AJAX whenever you need to get the contents of it.
本文标签: HTML textarea use JavaScript to get wrapped textStack Overflow
版权声明:本文标题:HTML textarea: use JavaScript to get wrapped text? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741978609a2408267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论