I am trying to get value of a textarea with line break. When I debug, the value is this way in jquery. the value stored in a variable like this:
"test<br>
test<br>
test"<br>
In .value of valueOf is: 'test↵test↵test'.
I wonder how can I convert it to \n in order to insert line break.. I'm using jquery to get the value and send by ajax to php!
thanks.. :)
Sorry my english..
Try this
val=document.getElementById('recommend').value;
val = val.replace(/<br\s*\/?>/mg,"\n");
Fiddle http://jsfiddle.net/eSub4/1/
I have put a couple of console.log in fiddle to show you how new line(\n) and html line break show up in console and compare it with the text from textarea to see that you are getting new line (\n) for text in textarea after using the regex. Keep firebug open to see the output
try
document.getElementById('textareaid').innerHTML;
you need to replace 'textareaid' with the actual id.
since you say you already have the data in a string, but its not formatted right, to turn the <br> into newlines, you can use this
textString=textString.replace(/<br>/g,"\n");
It works for me....
$("#id").val("<?php echo str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br>",$value); ?>".replace(/<br\s*[\/]?>/gi, "\n"));
Related
I have a text.txt file which contain tag name:
tagforinserttext
And I have a page with next code
<div class="header">
<h1 id="tagforinserttext"></h1>
</div>
In JS code I read file and get "tagforinserttext" into variable "tag".
Then I try to insert some text, using
document.getElementById(tag).innerHTML = "sometext";
But text now inserted...
I found that getElementById(tag) don`t have a link to tag in page, but if I write getElementById('tagforinserttext') then its work!
Then I tried to compare the variable with the text and got that they are not equal ... they are equal, because the text that is in the variable when output via alert () is equal to 'tagforinserttext'
I can`t understand where I make a mistake and I hope for your help.
From the comments I informed you to do
console.log(escape(tag))
which shows you have %0D which is a line break so you have a line break in the file you are reading. So you can remove the line break from the file or use a regular expression to remove the line break.
tag = tag.replace(/\r?\n|\r/g,"")
A better solution, use JSON so you can parse and get the string out easier.
I have a textarea HTML5 input with cols=120 set. When a user types a long string on it, the string may get wrapped into several lines, to fit the specified columns.
However, when I try to retrieve the text value of the element, the long string is not with any kind of line breaks associated.
Is there a way to get a string representation from the textarea which includes linebreaks exactly where the long string got wrapped, so that it goes the exact same way it was displayed into the screen?
You can use <textarea wrap="hard" ... to submit a form including line breaks as it appears to the user.
Use .val() to get value of textarea and use $.trim() to empty spaces.
$(document).ready(function () {
var val = $.trim($("textarea").val());
if (val != "") {1
$('div').append('<pre>'+val+'</pre>');
}
});
Demo : jsfiddle
Seems promising.Will test it here.
finding "line-breaks" in textarea that is word-wrapping ARABIC text
Thanks SO Related answers!
I have a Textarea which is in a form that has this value
Hello<div class="author"><br /><i> Written By admin </i></div>
I need to remove everything in the author class on load cause i will be adding the same updated data on submit.
You can simply replace that piece with regex:
textarea.val(textarea.val().replace(/<div class="author">.*?<\/div>/, ''));
Demo: http://jsfiddle.net/TimWolla/Nzvy7/
Probably not the answer you are looking for, but how exactly do you add text to the textarea on submit, and why don't just use the same method for removing text.
On the other hand doing:
$('#textareaID').val('Hello');
Will give you exactly the same result as removing the rest of the text and just keeping 'Hello'.
It's not exactly clear from the question, but to remove the <div class="author"> and its contents you can use split and shift:
$("textarea").val().split("<div class=\"author\"").shift();
http://jsfiddle.net/xtnL7/1/
I really tried this a lot but I haven't got any result that actually worked.
I want to read the text of html textarea line by line using JQuery. Textarea has a fixed width and height. For example if the text area has the text like:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br/>
aaaaaaaaaaaaa just typed a real long<br/>
word that wont fit in side of this part of<br/>
div
I would like to have a string as
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa< br /> aaaaaaaaaaaaa just typed a real long< br />
word that wont fit in side of this part of <br />div
I've tried it and it did not work out. Any suggestions?
You can use JavaScript's split function on the newline character \n. Then, call join on the resulting array.
Working example: http://jsfiddle.net/XjN72/1/
You can just remove the newlines:
text = text.replace(/\n|\r/g, "");
I have a <textarea> that I want to print the contents on the page below it. When it does this I want to make random words be omitted.
I have accomplished this. My problem is I want make it recognize when the enter button has been pushed in the <textarea> and display that below. I want those to not ever be omitted. I have gotten it so that when a <br/> is typed into the <textarea> it will not omit those and it will show the new lines. When I tried doing that with the \n it does not seem to recognize in the .value of the object that the enters are \n.
Any ideas how to fix this?
I also tried var txtAdd = document.getElementById('textLoc').value.replace("\n","<br/>"); and it did not work.
Maybe ".replace(/\n/g, '<br />');"?
I don't follow what you are trying to do, but is it possible that you need \r\n since that is the equivalent of newline on a windows machine (assuming you are running windows)?
I think its about way of saving the data first time you should use innerHTML property that you can save it with with out replacing with \n
Regards
Marwan