javascript - Tabs appear in textarea -
when user clicks on <p>
element want text in textarea
element. problem text appears in textarea beneath 1 blank row , on top of shifted right.
why happening?
i tried put textarea div , fix positioning didn't work.
i want achieve pure *javascript** , if needed css.
here's have far:
<!doctype html> <html> <head> </head> <body> <p id="p1"> text text text text text text text text text text text, text text text text text text text text text text text, text text text text text text text text text text text, text text text text text text text text text text text. </p> <div id="holder"></div> <script type="text/javascript"> var text_to_copy = document.getelementbyid('p1').innerhtml; var holder = document.getelementbyid("holder"); var input = document.createelement("textarea"); document.getelementbyid('p1').onclick = function(){ input.id = "textarea_id"; input.style.display = "block"; input.style.padding = "0px"; input.style.margin = "0px"; //input.style.textalign = "left"; not input.style.width = "562px"; input.value = text_to_copy; alert(input.value);//just see if onclick works holder.appendchild(input); } </script> </body> </html>
you replace multiple tabs , whitespaces (which appear due formatting) single whitespace before setting value of textarea, like:
input.value = text_to_copy.replace(/\s{1,}/g, ' ');
demo
explanation
what see regular expression pattern. \s
special character matches single white space character (including space, tab, form feed , line feed). g
flag , stands global search, meaning expression match occurrences. {1,}
means match preceding character 1 or multiple times. it's equivalent +
.
Comments
Post a Comment